change directory if available


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change directory if available
# 1  
Old 03-15-2011
change directory if available

I have a simple shell script that prompts the user to enter a directory to navigate to.
What i want it to do and i don't know how to do this is if the directory is invalid automatically navigate to the home directory.

Code:
echo "enter a directory to navigate to:"
read directory

cd $directory

could i use the eval command to see if its a valid directory and if not go to home directory automatically
cd ~

im not sure how to test to see if the directory is invalid..
# 2  
Old 03-15-2011
how about cd $directory
and then check $?, if >0, send him to Home.
# 3  
Old 03-15-2011
I suppose you must use this sourced, since a cd in a subshell does not change the parent:
Code:
while [ 1 ]
do
 echo "Enter a directory to navigate to: \c"
 read d
 if [ ! -d "$d" ]
 then
   echo "From '$pwd', entry '$d' is not a directory."
   continue
 fi
 if [ ! -r "$d" ]
 then
  echo "From '$pwd', entry '$d' is not readable."
  continue
 fi
 .
 .
 .
done

This User Gave Thanks to DGPickett For This Post:
# 4  
Old 03-15-2011
Try this:
Code:
( [ -d $directory ] && [ -x $directory ] ) || directory="~"
cd $directory


Last edited by Franklin52; 03-16-2011 at 06:21 AM.. Reason: Please use code tags, thank you
# 5  
Old 03-15-2011
or read the return value of your
Code:
cd $directory

if not 0, bring the user to home.

btw, why my reply didn't show?
# 6  
Old 03-15-2011
thank you dg

Last edited by icelated; 03-15-2011 at 02:47 PM..
# 7  
Old 03-15-2011
Quote:
Originally Posted by icelated
what if the user enters ~ or home???
whatever user entered, you execute
Code:
cd [entered value].

then check
Code:
$?

.
if it was "~". $?=0, you don't have to do anything.
this works even if you say "~" is not a valid directory. what shall u do then? bring him to home directory, right? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change directory shell

#!/bin/bash echo -n "Enter number of sanger patients : "; read id perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo")' < file.txt I have the above script... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Change Directory

Hi All, There is a code like below in my script ############################################### ###Create Directories and Sub-Directories ############################################### dpdir=DP_FROM_${from}_TO_${to} mkdir $dpdir cd $dpdir mkdir AWQM WFCONTROLLER PROVCO PRISM ... (1 Reply)
Discussion started by: pvmanikandan
1 Replies

3. UNIX for Dummies Questions & Answers

Change permission to a directory

Hi, How do i change the permission to read/write to a windows directory? (1 Reply)
Discussion started by: lg123
1 Replies

4. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

5. UNIX for Dummies Questions & Answers

How to change database directory to another directory?

Hi, I Installed mysql on my CentOS 6.2 Server. But when I tried to change the location of /var/lib/mysql to another directory. I can't start the mysql. Below is what I've done yum install mysql mysql-server mysql-devel mkdir /path/to/new/ cp -R /var/lib/mysql /path/to/new chown -R... (1 Reply)
Discussion started by: ganitolngyundre
1 Replies

6. AIX

disallow change directory

Dear all expects, I have a security problem that I would like to resolve. I need to create a user ID in my AIX 5.3 environment and to point the login to a specific directory for FTP purposes. There is only 2 directories that I can allow the user ID to perform read/write. I would like to prevent... (2 Replies)
Discussion started by: kwliew999
2 Replies

7. UNIX for Dummies Questions & Answers

Filenames change in a directory

Hi I have abc_ahb_one.v abc_ahb_two.v abc_ahb_three.v ........l like this -----upto abc_ahb_ninety.v in some directory. I need to change those file names to like below. ... (5 Replies)
Discussion started by: praneethk
5 Replies

8. UNIX for Dummies Questions & Answers

Change Directory

I have a directory that is existing under my root dir of the FTP server. The DIR name is 'Software Patch'. I want to move in to that DIR to download some patches. But, when I issued a command 'cd SOftware Patch', the system said that it cannot find the dir 'Software'. I tried all possible ways like... (2 Replies)
Discussion started by: vskr72
2 Replies

9. Shell Programming and Scripting

change directory

hi, Iam in directory A. I run a script from there. inside the script i have a command cd B. When i come out of the script directory is A only. Even when i come out scrip i want the directory to be B How to achieve (2 Replies)
Discussion started by: mkan
2 Replies

10. Shell Programming and Scripting

change directory

Hi all, I'm trying to wirte a small shell script in Linux. My script has the flow like, cmd1 cmd2 cd testdata cmd3 After exiting the program, the CWD remains the same as where I execute the program. I need it to be changed to the latest updated directory in the program. How can I do... (1 Reply)
Discussion started by: vadivel
1 Replies
Login or Register to Ask a Question