1752564 Members
4848 Online
108788 Solutions
New Discussion юеВ

common problem

 
neocosmic
Occasional Contributor

common problem

1. how to determine whether or not a command was completed successfully?

2. how can we make sure the variable is not reset?

3. what command can be used in script to lock terminal?
5 REPLIES 5
Balaji N
Honored Contributor

Re: common problem

1. depends on your shell and program. normally all programs return a value of 0 on successful execution and a non-zero error code for any errors during execution.

and based on youre shell, use an echo $? (ksh, bash, sh etc) and echo $status (csh and tcsh (?)) to know the status.

2. not very clear. variables are there to be set and reset. use set or setenv or export based on your shell and if u need a local or a global variable.

3. how abt scrolling through a loop and read for a passwd and wait until the correct passwd is entered.

hth
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
neocosmic
Occasional Contributor

Re: common problem

how can ignore the interrupts from other command, while im writing a script to lock terminal?
Stuart Browne
Honored Contributor

Re: common problem

1) expanding upon what Balaji said.

If a command completes successfully, it returns a 0 exit.

In most shells (bash, ksh, ash, sh) you can do conditionals upon the exit state, without having to do an 'if' check on the exit value. For example:

grep -q root /etc/passwd && echo "I found root in /etc/passwd"

When 'grep' successfully find a pattern in a file, it will return 0. The '&&' operator states that if there is a successful exit, that the following command should be executed (in this case a simple 'echo').

Using {} and () allows you to make this more complicated.

Other things you can do is to make 'if' descisions directly based upon the exit of a program, rather than checking the $? exit values, for example:

if grep -q root /etc/passwd
then
echo "I found root in the passwd file"
fi

This can also be negated:

if ! grep -q root /etc/passwd
then
echo "I couldn't find root in the passwd file"
fi

Most people however use the program 'test' to do such checking. '/usr/bin/test' is usually symbolically linked to '/usr/bin/[' (thus the format:

grep -q root /etc/passwd
if [ $? -gt 0 ]
then
echo "I couldn't find root in the passwd file"
fi
)
... umm, sorry .. ramble ..

2) Using ksh and bash, you can use the command 'typeset' (in-built shell-function), with the flag '-r' to mark a variable 'read-only'. It cannot be changed or un-marked as read-only, until the shell is destroyed.
One long-haired git at your service...
Stuart Browne
Honored Contributor

Re: common problem

use the inbuilt 'trap' command to trap signals.

trap "" 1 2 15

makes your script not respond to a SIGHUP (1), a SIGINT (2, ctrl-c) or SIGTERM (15).
One long-haired git at your service...
Balaji N
Honored Contributor

Re: common problem

trap [-lp] [arg] [sigspec ...]
The command arg is to be read and executed when the shell
receives signal(s) sigspec. If arg is absent or -, all speci-
fied signals are reset to their original values (the values they
had upon entrance to the shell). If arg is the null string the
signal specified by each sigspec is ignored by the shell and by
the commands it invokes. If arg is not present and -p has been
supplied, then the trap commands associated with each sigspec
are displayed. If no arguments are supplied or if only -p is
given, trap prints the list of commands associated with each
signal number. Each sigspec is either a signal name defined in
, or a signal number. If a sigspec is EXIT (0) the
command arg is executed on exit from the shell. If a sigspec is
DEBUG, the command arg is executed after every simple command
(see SHELL GRAMMAR above). If a sigspec is ERR, the command arg
is executed whenever a simple command has a non-zero exit sta-
tus. The ERR trap is not executed if the failed command is part
of an until or while loop, part of an if statement, part of a &&
or || list, or if the command's return value is being inverted
via !. The -l option causes the shell to print a list of signal
names and their corresponding numbers. Signals ignored upon
entry to the shell cannot be trapped or reset. Trapped signals
are reset to their original values in a child process when it is
created. The return status is false if any sigspec is invalid;
otherwise trap returns true.



man sh or ksh and search for trap.
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.