- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Issue with "if" statement
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
тАО12-14-2006 06:37 AM
тАО12-14-2006 06:37 AM
Issue with "if" statement
We are using below given "if" statement in one of our UNIX sh scripts.
if [ -f ${DIR}/Suspect_* -o -f ${DIR}/Prospect_* -o -f ${DIR}/Contact_* -o -f ${DIR}/Activity_* ]
then
echo "Yes"
else
echo "No"
fi
But it gives foolowing syntax error.
"ksh: test: syntax error"
But it is also observed that same sh script runs fine on other UNIX server.
when the above given "if" statement is changed to as given below it runs fine.
if [ -f ${DIR}/Suspect_* ] || [ -f ${DIR}/Prospect_* ] || [ -f ${DIR}/Contact_* ] || [ -f ${DIR}/Activity_* ]
then
echo "Yes"
else
echo "No"
fi
What could be the issue? Why would one command run fine on one server and not on other?
Please provide any information related to this.
Thanks in advance.
- Tags:
- Test
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 06:42 AM
тАО12-14-2006 06:42 AM
Re: Issue with "if" statement
I know that when I have anything in square brackets I have to make sure to have a space after the first and a space before the last or I get errors.
IE: [-f file.txt] <- I can't do this
[ -f file.txt ] <- this works
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 07:12 AM
тАО12-14-2006 07:12 AM
Re: Issue with "if" statement
now, my question to you is, does the second version work properly on the first server?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 09:11 AM
тАО12-14-2006 09:11 AM
Re: Issue with "if" statement
If you have a Suspect_1 and a Suspect_2, that would expand to "if [ -f Suspect_1 Suspect_2 -o ... ]", which isn't valid syntax. However, "if [ -f Suspect_1 Suspect_2 ]" will work fine because test will just ignore everything after the first filename (at least on HP-UX). And, of course, if you have exactly one matching file it works either way.
If you don't have any Suspect_* files, it could be evaluated as a blank or as the literal string "Suspect_*", depending on which shell you're using and what options are set. That's probably not what you want, but it just happens to work in HP's sh and ksh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2006 11:16 PM
тАО12-14-2006 11:16 PM
Re: Issue with "if" statement
in addition to the remarks of Heironimus:
You CAN use filename pattern as argument in a test statement in the ksh, when you change
[ .. ] to [[ ... ]]
Check the manpage for the correct syntax!
Changing your example to
if [[ -f ${DIR}/Suspect_* || -f ${DIR}/Prospect_* || -f ${DIR}/Contact_* || -f ${DIR}/Activity_* ]]
then ...
should do it.
mfG Peter