Check the exist status of the cd command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Check the exist status of the cd command
# 1  
Old 10-04-2011
Check the exist status of the cd command

Hi,
As in scripting , some cd commands getting failed, so we do check the exist status as 0 or 1. But every time we call to function for it. But does any single line exist will do the job with || , && ? i.e
Code:
ls -l
Logs
cd Logss | exit
echo hi

as Logss is not exist , Before printing "hi" we have to exit with some friendly output i.e "directory is not exist"

Thanks
# 2  
Old 10-04-2011
I'm not sure I understand your question, but you can use the control operators || and && with cd.
Code:
cd $somedirectory 2>/dev/null || echo "directory $somedirectory does not exist"; exit
echo hi

# 3  
Old 10-04-2011
Code:
 sh test.sh
cd Temp > /dev/null || echo "direcotry not exist" ; exit
+ cd Temp
+ exit

For failure case it works fine but for true cases also exit is executing. I had tested as earlier in this case
Linux-x86_64 GNU/Linux
# 4  
Old 10-04-2011
Try
Code:
cd $somedirectory 2>/dev/null || echo "directory $somedirectory does not exist" && exit

# 5  
Old 10-04-2011
There were curly braces missing :
Code:
cd $somedirectory 2>/dev/null || { echo "directory $somedirectory does not exist"; exit; }
echo hi

EDIT: this is bash syntax

Last edited by cero; 10-04-2011 at 12:09 PM..
# 6  
Old 10-04-2011
@cero
it works fine for me. Thanks. Actually i missed the exit ;(semicolon) so failed to getting the exact result. :-)
@jim
Already i quoted same thing in my script but not working for me. So trying multiple combination of the Logical AND , OR and Bitwise AND, OR condition to achieve it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

2. UNIX for Dummies Questions & Answers

Check Success Status Of sed command

How do i check success status of a sed command execution i have the below script not sure if it is right approach to check status of execution using a function. Also it looks like in the below sed command even if the search string doesn't exist in the file it is returning status as success as i... (6 Replies)
Discussion started by: Ariean
6 Replies

3. Shell Programming and Scripting

Determine the exist status

Hi, i am a novice in scrpting and need your help on the following. i have a script that checks if a file exists: if ; then echo " Files exists" else echo " file not found" fi assuming the files does not exist ,, the script when run dusplays " file not found".. so far true. but when i... (2 Replies)
Discussion started by: sbk785
2 Replies

4. Shell Programming and Scripting

Propagate exist status from a child shell script to a parent.

Hi All, I have a parent shell script A and a child shell script B. 1). If a command i.e a mysqdump fails in shell script B fails then I trap the error with this code if ] then func_exit "Failed to export the cleaned DB1.${MYDBNAME} database to the ${MYARCHIVEDIR} directory"... (1 Reply)
Discussion started by: daveu7
1 Replies

5. Shell Programming and Scripting

Check if file exist

Hi, I created following script to check if file exist: #!/bin/bash SrcDir=$1 SrcFileName=$2 SrcTimePeriod=$3 if ;then echo 1 else echo 0 fi I ran it like: /apps/Scripts/FileExist.sh /apps/Inbox file1 2nd_period_2010 Even file exist at that location, my above command is... (4 Replies)
Discussion started by: palak08
4 Replies

6. Shell Programming and Scripting

How to check status of tar command?

Hi, How to check the status of tar command using bash script ? I have the following tar command, how to check the status of the tar? tar -cjf $today.tar.bz2 /opt/data Regards, Eye Gee (1 Reply)
Discussion started by: egkua
1 Replies

7. Shell Programming and Scripting

How to check status of last print command?

I am working on an Linux based application where I am using lp -onobanner -s -d$RPTDEST command to print the file on desired printer. Variable $RPTDEST could be different each time even for the same user. I need to implent the check if last print command was succesful or not, so that application... (3 Replies)
Discussion started by: dpmore
3 Replies

8. Shell Programming and Scripting

check the directory exist

I have the below script to check whether directory is exist or not , now I sure the directory /abc NOT exist , but when run the script , it still pop the result is "the directory exist" , could suggest what is wrong ? thx ll -d /abc > /dev/null 2>&1 if then echo "the directory exist !!" ... (7 Replies)
Discussion started by: ust
7 Replies

9. Shell Programming and Scripting

Failed to check status code in "rsh" command

Hi folks, I wrote a ksh program which run scripts from remote server. To check the status code I wrote the following function: check_remote_status() { status_code=`tail -1 $installLog` if ] ; then echo $errMsg | tee -a $installLog exit 1 else echo $validMsg >> $installLog fi... (9 Replies)
Discussion started by: nir_s
9 Replies

10. Shell Programming and Scripting

check the status and send an email with status

Hi, We have a text file which has the following data. ISA~00~ ~00~ ~ZZ~VISTN ~ZZ~U1CAD ~051227~183 7~U~00200~000011258~0~P~< GS~FA~EE05J~U1CAD~051227~1831~000011258~X~002002 ST~997~0001 AK1~SH~247 AK2~856~2470001 AK5~A AK2~856~2470002 AK5~A... (3 Replies)
Discussion started by: isingh786
3 Replies
Login or Register to Ask a Question