cmd()
{
results=`eval $* 2>&1`
val=$? #return the $* execution value
}
cmd "cd /home/tom || exit 0"
how to realize two point
- it cd to "/home/tom"
- not exit from the script after the ---->
cmd cd /home/tom || exit 0
cmd()
{
results=`eval $* 2>&1`
val=$? #return the $* execution value
}
cmd "cd /home/tom || exit 0"
how to realize two point
cmd cd /home/tom || exit 0
It is very simple. If you don't want to exit; don't issue an exit
command; or be sure that the file /home/tom
exists, is a file of type directory, and that the user invoking cmd
has permission to cd
into that directory.
That's not what I wanted, I just give the example that I need to exit 0 intentionally, can you help me to re-write the cmd function
to achieve this ?
I'm not sure what you want to achieve. The command substitution in backticks will run in a subshell, so this subshell will be exited and wont harm neither the function nor the main script. Surprisingly the exit code will be assigned to val. Any "command string" will be valid for the subshell only, so even if the cd
succeeded, it will be lost for both function and script.
I've found the command exec sh
useful for situations when you need to switch the shell from running script commands to running interactively in the same context.