How to check exit status of unset variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check exit status of unset variables
# 1  
Old 02-02-2011
How to check exit status of unset variables

Hi All,

Is there any way to check exit status of unset variables?

In the following code PathX is not set and the script terminates without checking exit status.

Code:
#!/bin/bash

Path="/tmp/log"

cd ${PathX:?}

if [ $? -ne 0 ];then
   echo "Exit Status : non zero"
else
   echo "Exit Status : zero"
fi

Thanks in advance
# 2  
Old 02-02-2011
This code does not make sense to me. Rather than how you think it should be done, please explain what you want to accomplish.

To check if a variable is null or unset:
Code:
if [ ! -z "$var" ] ; then
  echo 'variable is defined'
else
  echo 'variable is not defined'
fi

Note: keep the quotes around the variable in the [ ] expression.
# 3  
Old 02-03-2011

A variable may have contents, be set but empty, or be unset:
Code:
if [ -n "$var" ]
then
  printf "Variable contains %s\n" "$var"
elif [ -n "${var+X}" ]
then
  printf "\$var is set but empty\n" 
else
  printf "\$var is not set\n"
fi


I use the following code in bash:
Code:
varstatus()
  if [ -n "${!1}" ]
  then
    printf "%s contains %s\n" "$1" "${!1}"
  elif [ -n "${!1+X}" ]
  then
    printf "%s is empty\n" "$1"
  else
    printf "%s is not set\n" "$1"
  fi

A sample run:
Code:
$ unset XYZ
$ varstatus XYZ
XYZ is not set
$ XYZ=
$ varstatus XYZ
XYZ is empty
$ XYZ=qwerty
$ varstatus XYZ
XYZ contains qwerty

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. Shell Programming and Scripting

Listing all local variables for unset

I have tried to thoroughly search other threads before posting this question... I have a shell script (bsh) that I'd like to "re-execute" if the user chooses to. Before the program is executed again the local variables (those set within the script) need to be unset. Is there a command that... (6 Replies)
Discussion started by: powwm
6 Replies

3. Shell Programming and Scripting

Unset variables in shell when it running two different loops

I have a script to start/stop/restart the tomcat application. When we run the script first time i.e stop/start it set all env variables(DISTRIB_ID,NAME,TOMCAT_CFG,....etc),but when we restart the tomcat it is running in the same shell.....I need to set the variables when i restart the tomcat(in the... (1 Reply)
Discussion started by: praveen265
1 Replies

4. Shell Programming and Scripting

[ask] about unset variables

I'm wondering, is the number of variables will affect execution time of my bash script or maybe affect the cpu workload, cpu memory, etc ? If I create so many variables, should I unset each one of that variables after I used them or after I think they are no longer needed? and if my script... (2 Replies)
Discussion started by: 14th
2 Replies

5. Shell Programming and Scripting

Check the exit status in a pipe call

Guys, I have a problem :confused: and I need some help: I've to process many huge zip files. I'd code an application that receive the data from a pipe, so I can simple unzip the data and send it (via pipe) to my app. Something like that: gzip -dc <file> | app The problem is: How can I... (7 Replies)
Discussion started by: Rkolbe
7 Replies

6. Shell Programming and Scripting

Check for exit status

Hi I have following code I want If whole code executes successfully then return true If found any error then print the error I tried if ; then But this checks only for the just upper line execution #!/bin/bash PATH1=/var/log/mysql PATH2=/home/ankur/log FILE1=mysql-bin.index... (4 Replies)
Discussion started by: kaushik02018
4 Replies

7. Shell Programming and Scripting

How to unset all variables in shell?

can I use unset to unset all the variables in a shell sciprt? VAR1=1 VAR2=2 VAR3=3 unset whether this unset will afftect any system variables? Thanks, (3 Replies)
Discussion started by: balamv
3 Replies

8. Shell Programming and Scripting

check exit status of bg scripts

HI All, I am running one shell script, in that script i am calling 4 scripts in the background. abc.ksh & efg.ksh & xky.ksh & mno.ksh & please let me know, how could i find the success and failure of each script. i Cannot use $?, because i want to run all the scripts in parellel. ... (2 Replies)
Discussion started by: javeed7
2 Replies

9. Shell Programming and Scripting

check exit status - Expect Script

from my main script, i am calling an expect script. there are a lot of conditions in the Expect script and it can have any exit value based on success or failure of the Expect Script. how can i check the exit status of Expect scritp in the main script. (1 Reply)
Discussion started by: iamcool
1 Replies

10. UNIX for Dummies Questions & Answers

how to check exit status in awk script

Hi, I have a main program which have below lines - awk -f test.awk inputFileName - I wonder how to check status return from awk script. content of awk script: test.awk --- if ( pass validation ) { exit 1 } else { (1 Reply)
Discussion started by: epall
1 Replies
Login or Register to Ask a Question