- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Error with script to compare two text strings
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
Discussions
Discussions
Forums
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
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
тАО11-29-2001 03:04 AM
тАО11-29-2001 03:04 AM
The script below tests to see if the value of the MONTH variable is "Jan". If so, it sets another variable called MON to 01 :
MONTH="Nov"
if test $MONTH="Jan"
then
MON=01
else MON=UNKNOWN
fi
echo "Month is $MONTH"
echo "Mon is $MON"
When I run this, I get :
Month is Nov
Mon is 01
I was expecting the value of Mon to be UNKNOWN but instead it is 01. In other words, the test condition in my if-statement is not working.
Does anyone know where this is going wrong?
Many thanks and regards,
Preet :-)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-29-2001 03:15 AM
тАО11-29-2001 03:15 AM
Re: Error with script to compare two text strings
Try this
MONTH=`date|cut -d " " -f 2`
if [ $MONTH = "Jan" ]; then
MON=01
else MON=UNKNOWN
fi
echo "Month is $MONTH"
echo "Mon is $MON"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-29-2001 03:18 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-29-2001 03:21 AM
тАО11-29-2001 03:21 AM
Re: Error with script to compare two text strings
MONTH="Nov"
if test $MONTH = "Jan"
then
MON=01
else MON=UNKNOWN
fi
echo "Month is $MONTH"
echo "Mon is $MON"
Notice the space before and after the = in the 'if test' statement.
Hope this helps.
-Santosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-29-2001 04:18 AM
тАО11-29-2001 04:18 AM
Re: Error with script to compare two text strings
MONTH="Nov"
MON=UNKNOWN
[ $MONTH = "Jan" ] && MON=01
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-29-2001 06:12 AM
тАО11-29-2001 06:12 AM
Re: Error with script to compare two text strings
you can also use a case construct:
case $MONTH in
Jan) MON=01;;
Feb) MON=02;;
Mar) MON=03;;
Apr) MON=04;;
May) MON=05;;
Jun) MON=06;;
Jul) MON=07;;
Aug) MON=08;;
Sep) MON=09;;
Oct) MON=10;;
Nov) MON=11;;
Dec) MON=12;;
*) MON=UNKNOWN;;
esac
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-29-2001 06:38 AM
тАО11-29-2001 06:38 AM
Re: Error with script to compare two text strings
the reason is the missing spaces, as all the others already told you...
But as an additional tip:
ALWAYS quote substitutions, e.g.
if [ "$MONTH" = "Jan" ]
...
case "$MAONTH" in
...
for the simple reason that one day your variable might be unset or plain empty and then you will get an syntax error message from the shell - but with quotes your script will still work.
Just my ???0.02,
Wodisch