0

how can i make this script actually change the directory i'm in?

so the script just takes some options from a database then after i select the one i want, it doesn't work i also tried running the script with source but still nothing..

    #!/bin/bash
    options=( $(mysql --skip-column-names -uroot -pmypass all_dbs -e "select path from databases_table WHERE locked != 0 ") )

    read_list(){
    echo ""
    PS3="Change directory to:"
    select opt in "${options[@]}" "Quit" ; do
        if (( REPLY == 1 + ${#options[@]} )) ; then
            exit
        elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
            chosen="$opt"
            break
        else
            echo "Invalid option. Try another one."
        fi
    done
    }

    changeto_entry(){
    mysql -uroot -pmypass --skip-column-names all_dbs -e "select name,\`database\`,path from databases_table where locked != 0 AND name = '$chosen'" | while read name database path; do
        cd $path
    done
    }

    read_list
    changeto_entry

3 Answers 3

1

You're trying to cd in a subshell created by a pipeline. Here's a simpler test case exhibiting the same problem:

true | cd /

To fix it, you can rewrite the pipe (cmd | while read; do ..; done) to use redirection from process substitution instead (while read; do ..; done < <(cmd)):

changeto_entry(){
  while read _ _ path
  do
    cd "$path"
  done  <   <(mysql -uroot -pmypass --skip-column-names all_dbs -e "select name,\`database\`,path from databases_table where locked != 0 AND name = '$chosen'")
}

Now, if your mysql command is correct, sourcing the file will change the directory.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 This is (almost certainly) the right answer. I kind of glazed over the pipe at the end of the long line. Of course the script will need to be sourced as well.
0

The current working directory will be for the script, not for you. i.e. your terminal user will just go back to normal after it executes.

2 Comments

is there any way to make it change my directory ?
I don't know of one, but imagine if every script you ran moved you around your filesystem, because the author forgot to put you back. pretty annoying.
0

Your script may work if you source it instead of run it as a normal script:

source ./mycdscript.sh

This will have the effect of evaluating the commands in script within the same shell as your bash prompt.

Be careful though, as any variables/functions set/defined in the script will still be defined in your prompt shell.

4 Comments

Are you sure the $path variable is getting set? Can you echo its value from within the while loop?
i used echo "cd $path" and i see it\s getting set
i encourage you to try the script - maybe replace the mysql with some array... it's very strange
Can you provide samples of what the options array might be set to, and sample output from the 2nd mysql command?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.