LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-17-2010, 10:03 AM   #1
Lilit83
LQ Newbie
 
Registered: Jun 2010
Location: Armenia
Posts: 7

Rep: Reputation: 0
Question Variables checking


Hello Guys,

Before script running I want to check if all variables are defined. If even one of them isn't define print variable name which should be defined. In other case continue to execute script.
 
Old 06-17-2010, 10:15 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by Lilit83 View Post
Hello Guys,

Before script running I want to check if all variables are defined. If even one of them isn't define print variable name which should be defined. In other case continue to execute script.
which sort of script is it?
 
Old 06-18-2010, 12:31 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,925

Rep: Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631Reputation: 2631
More accurately, which language? Some will instantiate vars on the fly when accessed.
 
Old 06-18-2010, 01:54 AM   #4
Lilit83
LQ Newbie
 
Registered: Jun 2010
Location: Armenia
Posts: 7

Original Poster
Rep: Reputation: 0
Bash script!
 
Old 06-18-2010, 03:45 AM   #5
Lilit83
LQ Newbie
 
Registered: Jun 2010
Location: Armenia
Posts: 7

Original Poster
Rep: Reputation: 0
I have written the following but it doesn't print the variable which is absent:

if [ "!`echo $VAR1`" ] || [ "!`echo $VAR2`" ] || [ "!`echo $VAR3`" ] || [ "!`echo $VAR4`" ] || [ "!`echo $VAR5`" ]; then
echo "Please define all variables"
exit
fi
 
Old 06-21-2010, 08:27 AM   #6
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by Lilit83 View Post
I have written the following but it doesn't print the variable which is absent:

if [ "!`echo $VAR1`" ] || [ "!`echo $VAR2`" ] || [ "!`echo $VAR3`" ] || [ "!`echo $VAR4`" ] || [ "!`echo $VAR5`" ]; then
echo "Please define all variables"
exit
fi
use


Code:
if [ -z $VAR ]; then
   do this or that
 
Old 06-21-2010, 08:45 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
Quote:
Originally Posted by centosboy View Post
if [ -z $VAR ]; then
do this or that
That's correct, but take in mind that if the variable is not defined (null) the actual test performed by the shell is
Code:
[ -z ]
which is interpreted as evaluating a string (always TRUE). Problem arises when we test for the opposite using -n
Code:
[ -n ]
again the test will be TRUE even if the variable is null. To avoid these situations better to include the value in double quotes or even better to use double brackets:
Code:
[ -z "$VAR" ]
[[ -z $VAR ]]
[[ -z "$VAR" ]]   # this one being the strongest way
 
Old 06-21-2010, 10:01 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 732

Rep: Reputation: 75
Hi.

It may be shorter to use parameter expansion tests with default messages:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate test for unset variables.

# Uncomment to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
# Infrastructure details, environment, commands for forum posts. 
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p
set -o nounset

pe
pe " Results:"

x=1
: ${x:?" Must specify x."}

a=2
b=3
unset c

: ${a:?} && : ${b:?} && : ${c:?}

: ${a:?" Not defined, a."} && : ${b:?" Not defined, b."}

: ${y:?" Must specify y (your message here)."}

exit 0
producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39

 Results:
./s1: line 30: c: parameter null or not set
See man bash, the test:
Code:
${parameter:?word}
Display  Error if Null or Unset.  If parameter is null or unset, the
expansion of word (or a message to that effect if word is not  present)
is written to the standard error and the shell, if it is not interactive,
exits.  Otherwise, the value of parameter is substituted.

-- excerpt from man bash
and the command:
Code:
       : [arguments]
              No effect; the command does nothing beyond  expanding  arguments
              and  performing any specified redirections.  A zero exit code is
              returned.
However, the longer tests are better if you want to test all the variables, accumulating a count of test results, so that you can display all errors in a single pass.

Best wishes ... cheers, makyo

Last edited by makyo; 06-21-2010 at 05:05 PM. Reason: ( Edit 1: minor typo )
 
Old 06-22-2010, 02:20 AM   #9
Lilit83
LQ Newbie
 
Registered: Jun 2010
Location: Armenia
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks Guy, it has been very helpful
 
  


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
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 07:09 AM
NB Checking In dforce LinuxQuestions.org Member Intro 2 07-08-2009 05:42 PM
Checking variables in bash sharky Linux - Software 4 02-13-2009 09:07 PM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 01:30 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 12:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:07 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
Open Source Consulting | Domain Registration