Search This Blog

Monday, October 12, 2015

Unix Shell Scripting

Unix uses Shells to accept commands given by users.

Commonly used shells: SH(Bourne SHell) CSH(C SHell) and KSH(Korn SHell
Other shells: KSH is based on SH and so is BASH(Bourne again shell). TCSH(Extended C SHell) is based on CSH.

To check which shell is installed:  echo $SHELL

Shell's command line gives interface between users and UNIX Kernel.

Frequently used commands:
sudo su -
gzip -d xyz.gz
pwd
ls -a
cd /home
./sample.sh (to execute a file)
cp
mv
date "%y,%m,%d,%h"
whoami
ps -e | grep parse
kill
hostname
ifconfig



General:
Use semicolon(;) to separate commands, backslash(\) to continue the commands in new line and pipe(|) to pass the output of one command as input to another command.

Conditional Operators:
&& - A command runs only the previous command is successful
|| - A command runs only the previous command is failed.

Group Commands:
Braces ({})  -Command runs in current shell and returns one exit status for the entire block of the command with in the braces.

Round Brackets(()) - Commands execute is subshell as single block.

Redirection:
> - redirect the output of the command to a new file (ls  > output.log)
< - pass the file as input to the command (grep venkat )

>> - redirect the output of the command to append to the existing file

File Descriptors:
0 - standard input
1 - standard output
2 - standard error ( ls 2> log1.log) - output the error to log1.log file


Substitution & patterns:
Filename substitution:
* - match any string
? - match any single character
[abcd]- match any of these characters for a single character
[a-d] - match any of the characters in the range for a single character.

Command Substitution: Output of one command as input for another command(use single quotes)
cat 'find . -name venkat*.txt' (dump all the files starts with venkat that exists in current directory to the screen)


No comments: