- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - HP-UX
- >
- System Administration
- >
- Error with script to compare two text strings
-
- Forums
-
Blogs
- Hybrid Cloud
- Edge
- Data & AI
- Working in Tech
- AI Insights
- Alliances
- Around the Storage Block
- Behind the scenes at Labs
- Careers in Tech
- HPE Storage Tech Insiders
- Inspiring Progress
- IoT at the Edge
- My Learning Certification
- OEM Solutions
- Servers: The Right Compute
- Shifting to Software-Defined
- Telecom IQ
- Transforming IT
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Russia
- HPE Blog, UK & Ireland
- Blogs
-
Quick Links
- Community
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Contact
- Email us
- Tell us what you think
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Blogs
-
Information
-
English
- 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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- 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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- 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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
11-29-2001 03:18 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- 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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- 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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- 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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- 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
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2019 Hewlett Packard Enterprise Development LP