LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2015, 03:26 AM   #1
GONMAN1
LQ Newbie
 
Registered: Sep 2015
Distribution: SUSE / UBUNTU
Posts: 2

Rep: Reputation: Disabled
Question 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,

Last edited by GONMAN1; 09-09-2015 at 04:11 AM.
 
Old 09-09-2015, 03:40 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,881

Rep: Reputation: 4485Reputation: 4485Reputation: 4485Reputation: 4485Reputation: 4485Reputation: 4485Reputation: 4485Reputation: 4485Reputation: 4485Reputation: 4485Reputation: 4485
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 ??
 
Old 09-09-2015, 04:03 AM   #3
GONMAN1
LQ Newbie
 
Registered: Sep 2015
Distribution: SUSE / UBUNTU
Posts: 2

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-10-2015, 01:39 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,735

Rep: Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrontab Directory Change (watched Folder = Mounted FTP Account) kunalks Linux - Software 2 05-25-2015 11:18 AM
Using shell script how to switch user and change folder sureshpanchanathan Linux - General 1 02-10-2012 12:43 AM
Use Linux script to batch change file names in a folder jiapei100 Linux - General 3 11-09-2009 05:33 AM
Help with bash script to create directory to /home folder wegadnie Linux - General 5 10-25-2008 11:50 AM
Script to change folder permissions waitingforOES Linux - Newbie 1 03-07-2005 01:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration