- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell Script Help
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
05-08-2002 01:22 AM
05-08-2002 01:22 AM
Shell Script Help
I have a shell script written , having 2 nos of commnads to execute. Here I have to define a condition for the execution so that if the first command is successful then only second will get executed.
Pl help !!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:27 AM
05-08-2002 01:27 AM
Re: Shell Script Help
command 2 will only get executed if command 1 is successful. See man sh-posix for details.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:28 AM
05-08-2002 01:28 AM
Re: Shell Script Help
Do something like this.
#!/usr/bin/ksh
command1
if [ $? -eq 0 ]
then
command2
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:30 AM
05-08-2002 01:30 AM
Re: Shell Script Help
command1
if [ $? -eq 0 ]
then
command2
fi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:31 AM
05-08-2002 01:31 AM
Re: Shell Script Help
if command1
then command2
fi
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:37 AM
05-08-2002 01:37 AM
Re: Shell Script Help
In "if [$# -eq 0] then" what does "0" corresponds? I believe it's "false" so doesn't this condition mean "if exit status of my previous commnad is "false" then execute second one".
OR should I have to specify "1" instead of "0".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:38 AM
05-08-2002 01:38 AM
Re: Shell Script Help
So [ $? -eq 0 ] is a test of successful execuation of the preceding command.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:47 AM
05-08-2002 01:47 AM
Re: Shell Script Help
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x96b70bce6f33d6118fff0090279cd0f9,00.html

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:53 AM
05-08-2002 01:53 AM
Re: Shell Script Help
this is very easy, you even don't need to test anything like return code:
first_command && second_command
--> second command will only be executed, if first command has a return code value of zero
first_command || second_command
--> second command is only executed, if first command was not successful, has a return code not equal zero.
If second command is executed later in the script, you have to put the return code of first command as value into a variable, cause return code is overwritten each time, a new command is executed:
first_command
var=`echo $?`
Then, if the second command has to be executed, check out the return code of the first command:
if [ $var -eq 0 ]
then
second_command
fi
Allways stay on the bright side of life!
Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2002 01:59 AM
05-08-2002 01:59 AM
Re: Shell Script Help
u can check the success or failure of the command, include an echo statement.
when a command successfully executes "echo $?" should show "0" else it shows "1".
in ur case
command1
if [ $? -eq 0 ]
then
command2
fi
regds
ravi