Operating System - HP-UX
1753947 Members
7464 Online
108811 Solutions
New Discussion юеВ

Re: Scripting unix commands

 
Rose Marie Mansell
Occasional Contributor

Scripting unix commands

I am wanting to create a script that will save me some time and help prevent keystroke errors during recovery periods. I am wanting to execute several unix commands in a designated order but need to incoporate checks so that I know that a command completed successfully prior to moving onto the next command. The commands I am looking at are vgimport, vgchange, mount, vgcfgbackup. I am struggling with best approach...suggestions are appreciated.
2 REPLIES 2
IT_2007
Honored Contributor

Re: Scripting unix commands

If you better explain what exactly you want to automate then it would be easier to write script.

Ivan Ferreira
Honored Contributor

Re: Scripting unix commands

You can check the exit code for the command with:

echo $?

So, you can do somethig like:

RESULT=`command1; echo $?`

if [ $RESULT -eq 0 ]; then
echo "command executed correctly"
else
echo "Last command executed with errors"
exit 1
fi

RESULT=`command2; echo $?`

if [ $RESULT -eq 0 ]; then
echo "command executed correctly"
else
echo "Last command executed with errors"
exit 1
fi

.
.
.

RESULT=`commandN; echo $?`

if [ $RESULT -eq 0 ]; then
echo "command executed correctly"
else
echo "Last command executed with errors"
exit 1
fi
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?