Operating System - HP-UX
1833424 Members
3035 Online
110052 Solutions
New Discussion

Re: Help needed with case and if statements

 
SOLVED
Go to solution
Robert Legatie
Advisor

Help needed with case and if statements

Hello,

I have the following code that uses sftp to upload files and download files from a server.
server=pandora.test
port=\#2202
port=\#22
user="test"
pword="password"
action="initial"
put_dir="/Usr/test"
get_dir="."
upload_dir="/home/pmp/topmp"
download_dir="/home/pmp/frompmp"
log_name="messages"
day=$(date +%d)
print_date=$(date '+%m-%d-%y %H:%M')
mail_to=xxxx@gmail.com

if [ "X$1" != "X" ]
then
print " "
print "invocation date: $print_date"
print " "
fi
case $1 in


-u)

# different file formats for different dates

case $day in
03 | 04 | 05 | 06)
upfile="PMP.200*.txt"
;;

22)

upfile="PMP.200*.txt"
;;

*)
print "Script only intended to run in upload mode on 3rd through 6th of each month\n"
exit 1
;;
esac

print "Testing upload dir/file: $upload_dir/$upfile"
test -e $upload_dir/$upfile

if [[ $? = 0 ]]
then
cd $upload_dir
action="put"


for F in $upfile
do

(( send_trys = 1 )) # initialize upload loop variable
sent="false"
confirmed="false"


Can someone please help me understand the following:
if [ "X$1" != "X" ]
then
print " "
print "invocation date: $print_date"
print " "
fi

I know it is checking for a null condition but what exactly is it checking for.

Also there are two case statements but closed only once (esac).

Any help will be appreciated.

7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Help needed with case and if statements

Hi Robert:

> Can someone please help me understand the following: if [ "X$1" != "X" ]

The script is (as you said) testing to see if '$1' (the first argument passed to it on the command line) exists or not. I would have used the second technique in the snippet below:

#!/usr/bin/sh
if [ "X$1" != "X" ]; then
echo has_value
else
echo empty
fi

if [ ! -z "$1" ]; then
echo has_value
else
echo empty
fi

...

As for the two case statements with only one 'esac' for closure, that's a syntax error.

Regards!

...JRF...
Robert Legatie
Advisor

Re: Help needed with case and if statements

thanks for the response.
if [ ! -z "$1" ]; then
echo has_value
else
echo empty
fi

what are we checking? how does it fit in when the script is executed. has_value means?
Pardon my ignorance.
James R. Ferguson
Acclaimed Contributor

Re: Help needed with case and if statements

Hi (again) Robert:

> what are we checking? how does it fit in when the script is executed. has_value means?

I was trying to answer your question by example. The script snippet is checking to see if an argument was passed ($1) to it at runtime or not.

# cat ./showme
#!/usr/bin/sh
if [ "X$1" != "X" ]; then
echo "has_value=$1"
else
echo empty
fi

if [ ! -z "$1" ]; then
echo "has_value=$1"
else
echo empty
fi

# ./showme
empty
empty

# ./showme 22
has_value=22
has_value=22

Regards!

...JRF...
Robert Legatie
Advisor

Re: Help needed with case and if statements

James you did reply to my other thread as well to the days of the week script.

Could you please help me use that logic in the case statement below

case $day in
03 | 04 | 05 | 06)
upfile="PMP.200*.txt"
;;

22)

upfile="PMP.200*.txt"
;;

*)
print "Script only intended to run in upload mode on 3rd through 6th of each month\n"
exit 1
;;
esac

I need this script to run only through Monday to Friday of each week.

Thanks.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help needed with case and if statements

Hi Robert:

> I need this script to run only through Monday to Friday of each week

Your code could look something like this:

#!/usr/bin/sh
DAY=$(date "+%w")
case ${DAY} in
0 ) echo "Sunday --- skipping"
;;
6 ) echo "Saturday --- skipping"
;;
* ) echo "Goody --- a weekday!"
/usr/bin/date
;;
esac

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Help needed with case and if statements

>I need this script to run only through Monday to Friday of each week.

I would use a simple if vs case:
day=$(date +%u)
if [ $day -gt 5 ]; then
echo "not weekday"
exit
fi

>JRF: Your code could look something like this:

You can also use multiple values in a case label:
case ${DAY} in
0|6) echo "weekend --- skipping" ;;
James R. Ferguson
Acclaimed Contributor

Re: Help needed with case and if statements

Hi (again):

> Dennis: You can also use multiple values in a case label

Yes, TIMTOWTDI :-)

Regards!

...JRF...