- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: if ! not working in ksh
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
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
тАО03-13-2007 09:51 AM
тАО03-13-2007 09:51 AM
I can get the following to work on the command line if I type it in, but not in a script. In a script, it complains about the "!".
The script starts with #!/bin/ksh.
Why is that, is there a bug?
if ! grep "Synchronizing" $STATE
then
echo not
else
echo yes
fi
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-13-2007 10:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-13-2007 10:12 AM
тАО03-13-2007 10:12 AM
Re: if ! not working in ksh
I do not think you can use "if !"
typically you would either "if true" or "if false". Not "if not true."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-13-2007 10:24 AM
тАО03-13-2007 10:24 AM
Re: if ! not working in ksh
This should be:
if [ `grep -c "Synchronizing" $STATE` -eq 0 ]
...
That is, it appears that you want to test whether or not there are any occurances of "Synchronizing" in the (file) ${STATE}.
Another way is to examine the return value of grep (zero meaning that there *is* a match):
# grep "Synchronizing" ${STATE} && echo "yes" || echo "no"
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-13-2007 12:19 PM
тАО03-13-2007 12:19 PM
Re: if ! not working in ksh
Ooops. The last suggestion is best done with the '-q'uiet switch (since you don't want the output; only the success or failure of the match:
# grep -q Synchronizing ${STATE} && echo yes || echo no
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-13-2007 08:32 PM
тАО03-13-2007 08:32 PM
Re: if ! not working in ksh
Right, it is only available in [ ] or conditional expressions in [[ ]].
Or !(pattern-list).
And of course in arithmetic expressions in (( )) or let.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-13-2007 11:31 PM
тАО03-13-2007 11:31 PM
Re: if ! not working in ksh
this feature is not available in the standard ksh; in the ksh93 version, it is.
So if you want to try, use
/usr/dt/bin/dtksh
as the shell for your script.
mfG Peter
- Tags:
- dtksh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-14-2007 01:41 AM
тАО03-14-2007 01:41 AM
Re: if ! not working in ksh
Still not real clear as to why it works in shell but not in the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-14-2007 04:18 AM
тАО03-14-2007 04:18 AM
Re: if ! not working in ksh
well, it would appear that the shell you work in is not ksh.
easy test, type in /bin/ksh and then enter the command. does it work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-14-2007 04:43 PM
тАО03-14-2007 04:43 PM
Re: if ! not working in ksh
"man test" is your friend for this sort of stuff.
I tend to be a bit old school and tend to do something like:
grep -q "some string" "some file"
if [ $? -ne 0 ]
then
echo "some string not found"
fi
Because to me it makes it clear that it is the exit code that is being Analyzed in the conditional.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-14-2007 04:45 PM - edited тАО10-09-2011 08:54 PM
тАО03-14-2007 04:45 PM - edited тАО10-09-2011 08:54 PM
Re: if ! not working in ksh (need sh or dtksh)
>Peter: this feature is not available in the standard ksh
You're right, it is highlighted on my sh-posix(1) entry. ;-)
>OldSchool: it would appear that the shell you work in is not ksh.
Most likely, sh.
>Still not real clear ... but not in the script.
Change to: #!/bin/sh.
>Daavid: "man test" is your friend for this sort of stuff.
Not if you are using ksh or the posix shell. And test(1) is only for stuff in [...].
And more importantly, test(1) just invokes the posix shell.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-15-2007 12:16 AM
тАО03-15-2007 12:16 AM
Re: if ! not working in ksh
> Dennis wrote :Change to: #!/bin/sh.
To be clear, '/bin' is a link to '/usr/bin'. '/usr/bin/sh' is the POSIX shell in HP-UX. The Korn-88 shell is represented by '/usr/bin/ksh' and the Korn-93 shell can be found as '/usr/dt/bin/dtksh'. The (useless?) Bourne shell can be found as '/usr/old/bin/sh'.
Regards!
...JRF...