Results 1 to 5 of 5

Thread: Bash Menu Script

  1. #1
    Join Date
    Jan 2012
    Beans
    4

    Bash Menu Script

    HOMEWORK ASSIGNMENT
    I am creating a bash Menu script that would prompt a user to select options. Option 4 is for changing directory. I have accomplished this by using the following:
    change_directory () {
    read -p ' Please enter your directory :' dir
    cd "/$dir"

    But my question is, what if the user inputs a directory that isnt in the / path? How could I go from there? I have no clue in how to do that. So i need your help guys.
    thank you in advance

  2. #2
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    Hidden!
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: Bash Menu Script

    You should use an If statement and nest the cd command inside like so
    Code:
    if [ -d "/$dir" ]; then
        cd "/$dir"
    fi
    This allows you to only execute commands if the statement (the bit between [ and ] ) evaluate to true, in this case we ask bash if the statement [ is $dir a directory ] evaluates to true, if it is bash runs everything from "then" to "fi"
    To add alternative options there is also the "elif" option where you can specify a second possible statement, and the "else" option which is a failsafe incase none of the statements evaluate to true, for example
    Code:
    if [ $dir is in users home dir ]; then
        cd /$dir
    elif [ $dir is a directory outside of users home ]; then
        echo $dir isn't your folder
    else
        echo $dir isn't a directory
    fi
    If deliberately not used proper code here to keep it easy to read for beginners
    Last edited by CaptainMark; December 7th, 2012 at 11:17 AM.
    Catch me on Freenode - imark

  3. #3
    Join Date
    Jan 2012
    Beans
    4

    Re: Bash Menu Script

    Quote Originally Posted by CaptainMark View Post
    You should use an If statement and nest the cd command inside like so
    Code:
    if [ -d "/$dir" ]; then
        cd "/$dir"
    fi
    This allows you to only execute commands if the statement (the bit between [ and ] ) evaluate to true, in this case we ask bash if the statement [ is $dir a directory ] evaluates to true, if it is bash runs everything from "then" to "fi"
    To add alternative options there is also the "elif" option where you can specify a second possible statement, and the "else" option which is a failsafe incase none of the statements evaluate to true, for example
    Code:
    if [ $dir is in users home dir ]; then
        cd /$dir
    elif [ $dir is a directory outside of users home ]; then
        echo $dir isn't your folder
    else
        echo $dir isn't a directory
    fi
    If deliberately not used proper code here to keep it easy to read for beginners
    thank you very much for your explanation. I do get your code, What I dont get is: How do I create a condition that would state that $dir is in my Home directory?
    I have tried if [ $dir = $HOME ]; then
    cd "$HOME"

    I am definitely doing something wrong, since I am a novice in this.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,364
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash Menu Script

    $HOME is /home/yourusername
    $dir is dir

    glue these 2 together with / to form the full path and check with -d its existence.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Jan 2012
    Beans
    4

    Re: Bash Menu Script

    Quote Originally Posted by Vaphell View Post
    $HOME is /home/yourusername
    $dir is dir

    glue these 2 together with / to form the full path and check with -d its existence.
    I just that and I came out with this code

    Code:
    change_directory () {
            read -r -p 'please enter your directory ' dir
            if [ -d "/$dir" ]
            then
                    cd "/$dir"; echo "You're in `pwd` and these are your files"
                    sleep 1.5; ls | more
            elif [ -d "/home/$dir" ]
            then
                    cd "/home/$dir"; echo "You're in `pwd` and these are your files"
                    sleep 1.5; ls | more
            elif [ -d "$HOME/$dir" ]
            then
                    cd "$HOME/$dir"; echo "You're in `pwd` and these are your files"
                    sleep 1.5; ls | more
            else
                    echo "$dir name does not exist"
            fi
            read -r -p 'Please [ENTER] to continue...'
            clear
    That gave me access to / and $HOME. And i probably am gonna leave it like that. I can't figure out how to cd to a different directory in different path, like for example: /etc/random or /home/user/Documents/random.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •