- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Help needed with case and if statements
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2009 06:09 AM
06-25-2009 06:09 AM
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.
Solved! Go to Solution.
- Tags:
- case
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2009 07:00 AM
06-25-2009 07:00 AM
Re: Help needed with case and if statements
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2009 07:18 AM
06-25-2009 07:18 AM
Re: Help needed with case and if statements
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2009 07:49 AM
06-25-2009 07:49 AM
Re: Help needed with case and if statements
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2009 09:42 AM
06-25-2009 09:42 AM
Re: Help needed with case and if statements
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2009 09:53 AM
06-25-2009 09:53 AM
Solution> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2009 04:18 PM
06-25-2009 04:18 PM
Re: Help needed with case and if statements
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" ;;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2009 04:28 PM
06-25-2009 04:28 PM
Re: Help needed with case and if statements
> Dennis: You can also use multiple values in a case label
Yes, TIMTOWTDI :-)
Regards!
...JRF...