LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Cannot change directory/folder in SCRIPT (https://www.linuxquestions.org/questions/linux-newbie-8/cannot-change-directory-folder-in-script-4175553007/)

GONMAN1 09-09-2015 03:26 AM

Cannot change directory/folder in SCRIPT
 
Based on user input, I try to redirect the user to two different locations. Here's part of the script:

# We will start to look after the files: to resend.
echo -e "\e[31;43m***** SEARCHING FILES *****\e[0m"
echo "Which files are you looking for? Is it an XXXXXXXXX (RFTSX) dealer or a XXXXXXXX (RMFT) dealer?"
read input_search
echo "You entered: $input_search"
sleep 2
#
#
# We are now turning to the desired directory.
echo -e "\e[31;43m***** CHANGING DIRECTORY *****\e[0m"
_changedir1(){
if [ $input_search -eq 'RFTSX' ]; then
function d1 { cd /XXXXXXXXXXXXX; }
fi
}
_changedir2(){
if [ $input_search -eq 'RMFT' ]; then
function d2 { cd /XXXXXXXXXXXXX; }
fi
}

I tried several and different syntax but none of them worked. Important to know is that I'm logged in as root and everything is done as root.

The script doesn't give me any error, but it stays on the same directory instead of changing to the ones I ask him.

Any idea which other options I could try?

Thanks,

pan64 09-09-2015 03:40 AM

I do not really understand that syntax:
Code:

_changedir1(){
if [ $input_search -eq 'RFTSX' ]; then
    cd /ARCHIVES/CONFDATA/RFTSX/AUTOLINE;
fi
}

why do you need that function d1 ??

GONMAN1 09-09-2015 04:03 AM

A simple cd doesn't work. That's why I thought to create a function. The $input_search captures the input of the user.
The purpose is that if the user answer something it will redirect him somewhere and if another answer is given somewhere else.

chrism01 09-10-2015 01:39 AM

1. '-eq' is the comparator for integers, not strings: see http://www.tldp.org/LDP/abs/html/comparison-ops.html for the table + examples.

2. If you are expecting the shell script to dump the user into another dir when it exits, it can't.
When a shell script runs, its effectively in a copy of the shell, so when it exits, it reverts to where it was called from.
If you 'source' it, that would do it.
Code:

source script.sh

# OR use '.'
. script.sh

3. try adding
Code:

set -xv
at the top to debug it


All times are GMT -5. The time now is 03:55 PM.