Unix Tutorial 16 : Command Line Arguments
While running a command, the user can pass a variable number of parameters in the command line.
Within the command script, the passed parameters are accessible using ‘positional parameters’. These range from $0 to $9, where $0 refers to the name of the command itself, and $1 to $9 are the first through to the ninth parameter, depending on how many parameters were actually passed.
Example:
$ sh hello how to do you do
Here $0 would be assigned sh
$1 would be assigned hello
$2 would be assigned how
And so on …
We will now look at some additional commands to process these parameters.
#1) set
Each process is also associated with a priority. This is used to ensure that the OS is able to fairly allocate time to various processing tasks. The ‘nice’ command can be used to reduced the priority of a process and thus be ‘nice’ to the other processes, i.e.
$ nice <command>
This line will run the specified command at a lower priority – by default, the priority will be reduced by 10. The command also takes a parameter that can be used to use a different level of ‘niceness’.
Example:
$ nice -20 ls
This command runs ‘ls’ with the priority reduced by 20.
It is also possible to increase the priority with a negative ‘niceness’. However, this needs superuser permission.
When a terminal or login session is closed, it sends the SIGHUP signal to thr child processes. By default, this signal will cause the child processes to terminate. The ‘nohup’ command can be used to allow commands to continue running even when the login session is terminated.
Example:
$ nohup soft file1 > file2
With this command, sorting of file1 and saving in file2 process will continue even if we have logged out of the system.
The ‘kill’ command can be used to terminate any of the processes depending on permissions.
Example:
$ kill [options] <pid>
This command will terminate a process with the process id <pid>. The PID of a process can be obtained using the ‘ps’ command.
This ‘at’ command is used to execute commands at a particular date and time in the future.
Example:
$ at 8pmsort file1>file2
Unix Debugging
Unix provides a number of mechanisms to help find bugs in your command scripts. These mechanisms can be used to view a trace of what is being executed i.e. the sequence in which commands are executed. The trace can be used to understand and verify the logic and control flow of the script.
=> set -v
verbose mode: Setting this option before running a command will ensure that the command that will be executed is printed to stdout before it is actually executed.
=> set -x
execution trace mode: Setting this option will display each command as it is executed along with its arguments.
=> set -n
no-exec mode: Setting this option shows any errors without actually running any commands.
Comments
Post a Comment