LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2006, 08:32 AM   #1
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Rep: Reputation: 15
Question How to check variables in a FOR loop?


Hello everybody,
I have this code for checking external environment variables:

Code:
...
...
   if [[ ! -d $EXTVARONE  || "$EXTVARONE" == '' ]]
   then
        cat /dev/null | mailx -s "ERROR:$SCRIPTNAME No Env var EXTVARONE" $MAILTO
        exit 1
   fi

   if [[ ! -d $EXTVARTWO  || "$EXTVARTWO" == '' ]]
   then
        cat /dev/null | mailx -s "ERROR:$SCRIPTNAME No Env var EXTVARTWO" $MAILTO
        exit 1
   fi
...
...
I try to put them in a FOR in order to review all the externals variable which uses my script.

I try something like:
Code:
ciclo='$EXTVARONE $EXTVARTWO ...ETC'
for i in $ciclo;
do
      # Cheching if the directory exist & variable isn't NULL
      if [[ ! -d $i  || "$i" == '' ]]
      then
          cat /dev/null | mailx -s "ERROR:$SCRIPTNAME No Env var \     
          EXTVARTWO" $MAILTO
          exit 1
      fi
done
I'm doing something wrong because I want $i to be $EXTVARONE $EXTVARTWO ...ETC on each loop and compare the value of each variable. I'm sure that it is a matter of how I declare the variable but until now I have been playing without success on this way.

Does anyone knows how to declare or assign my variable in the loop FOR?.
REGARDS
 
Old 07-06-2006, 08:36 AM   #2
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Original Poster
Rep: Reputation: 15
Sorry,
I forgot to mention it is on Kron Shell.
#!/usr/bin/ksh
 
Old 07-06-2006, 09:20 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
if you use:
Code:
#!/usr/bin/ksh -eu
any unset variables will cause an error message and exit.
so you could do:
Code:
#!/bin/ksh -eu

ciclo='$EXTVARONE $EXTVARTWO'
for i in $ciclo;
do
    eval : $i
done
Code:
billym.primadtpdev>rich.sh
rich.sh[6]: EXTVARONE: parameter not set
 
Old 07-06-2006, 10:14 AM   #4
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Original Poster
Rep: Reputation: 15
Question

Thanks bigearsbilly,

Your tip is just AMAZING!!!.
Now I need to find how to log the information into a log file in order to track what was wrong.

Because:
EVAL
3) Errors cause a script that contains them to abort.

Any idea?
 
Old 07-06-2006, 10:24 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
simplest is

> blah.log 2>&1 script.sh
2>&1 script.sh | tee blah.log

or put:
Code:
#!/bin/ksh -eu
exec > out.log 2>&1
at the top of the script

or execute it as a batch job and get a mail posted to you
is my preference
 
Old 07-06-2006, 12:58 PM   #6
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Original Poster
Rep: Reputation: 15
Thanks bigearsbilly,

In fact, I have both implement sending a mail and log in a file the trace of the execution.

I want to see the the information on the cosole and send also this information to the logfile.

After checking the logfile and directory I did this,but it does not work :

exec 2>&1 | tee -a $LOGDIR/$LOGFILE

$LOGDIR -> Directory for the logfile
$LOGFILE -> logfile to add info

? ?
 
Old 07-10-2006, 03:57 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
no, exec | tee
won't work unfortunately.
you'll have to run the script to a logfile
script | tee -a log

a way of doing it is to wrap in a function,
like
Code:
main()
{
   # script in here
}
time main | tee logfile
but it has pitfalls too.
I tend to Keep It Simple and just batch it, cron it or tee it.
You can always simply tail -f the log file.
 
  


Reply



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
shell variables becoming zero outside the loop cool244 Programming 4 05-20-2006 03:33 PM
shell variables losing value outside while loop cool244 Programming 1 05-19-2006 10:04 AM
Bash - Getting a for loop to process variables with a space in them davee Linux - General 4 10-08-2005 11:21 AM
BASH variables getting unset outside of loop trevelluk Programming 2 03-25-2005 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:41 AM.

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