Tuesday 20 May 2014

Linux Shell Programming

Cited from the book "Beginning Linux Programming"
"File descriptor 0 is the standard input to a program, file descriptor 1 is the standard output, and file descriptor 2 is the standard error output.

To direct error and standard output to the same file, using

kill -1 1234 >killouterr.txt 2>&1

Never use the same filename twice in a string of commands.

In globing, single-character wildcards using ?, while [set] allows any of a number of single characters to be checked. [^set] negates the set.

To check if files are scripts or not is to use the "file" command, for example, "file xxx.pl".

To make file executable, "chmod +x bbb.sh".

If the shell environment variable PATH isn’t set to look in the current directory for commands to execute. To change this, either type PATH=$PATH:.on the command line or edit your .bash_profile file to add this command to the end of the file; then log out and log back in again.

If the command is just for yourself, you could create a bin directory in your home directory and add that to your path. If you want the script to be executable by others, you could use /usr/local/bin or another system directory as a convenient location for adding new programs.

chown root /usr/local/bin/first
chgrp root /usr/local/bin/first

By default, all variables are considered and stored as strings, even when they are assigned numeric values. The shell and some utilities will convert numeric strings to their values in order to operate on them as required.

We can assign user input to a variable by using the read command. This takes one parameter, the name of the variable to be read into, and then waits for the user to enter some text.

$ IFS=’’
$ set foo bar bam
$ echo “$@”
foo bar bam
$ echo “$*”
foobarbam
$ unset IFS
$ echo “$*”
foo bar bam

The "["or "test" command is the shell’s Boolean check.

If you prefer putting thenon the same line as if, you must add a semicolon to separate the "test" from the "then".

String and arithmetic comparison is illustrated in page 33.

elif=>else if

If you want the echo command to delete the trailing new line, the most portable choice is to use the printf command rather than the echo command. Some shells use echo –e, but that’s not supported on all systems. bash allows echo -n to suppress the new line, so if you are confident your script needs to work only on bash, you can use that syntax.

The * wildcard expression doesn’t work within quotes.

The AND list construct allows us to execute a series of commands, executing the next command only if all the previous commands have succeeded. The syntax is statement1 &&statement2 &&statement3 &&...

if [ -f file_one ] && echo “hello” && [ -f file_two ] && echo “ there”
then
echo “in if”
else
echo “in else”
fi

"echo" always returns true.

The OR list construct allows us to execute a series of commands until one succeeds, then not execute any more. The syntax is statement1 ||statement2 ||statement3 ||...




No comments:

Post a Comment