Categories
Shell script

Pipes

A pipe is a way to connect the output of one program to the input of another program without any temporary file.

Syntax:
command1 | command2

eg: who | wc -l  

Output of who command is given as input to wc command So that it will number of user who logon to system

Pipe - Redirecting output of 1st command to 2nd without creating temporary file

Categories
Shell script

Command line arguements

eg:

$ myshell foo bar

 Shell Script name i.e. myshell
 First command line argument passed to myshell i.e. foo
 Second command line argument passed to myshell i.e. bar

In shell if we wish to refer this command line argument we refer above as follows

 myshell it is $0
 foo it is $1
 bar it is $2

Here $# (built in shell variable ) will be 2 (Since foo and bar only two Arguments), Please note at a time such 9 arguments can be used from $1..$9, You can also refer all of them by using $* (which expand to `$1,$2…$9`). Note that $1..$9 i.e command line arguments to shell script is know as “positional parameters“.


Categories
Shell script

Wildcards

Wildcard types:

1. *  – Matches any string or group of characters.

2. ?  – Matches any single character.

3. […]  – Matches any one of the enclosed characters

Note: 

[..-..] A pair of characters separated by a minus sign denotes a range.

Categories
Shell script

The read statement

Use to get input (data from user) from keyboard and store (data) to variable.
Syntax: 
read variable1, variable2,…variableN

 

eg:

#!/bin/sh

echo “Enter your name please”

read fname

echo “Welcome $fname”

Categories
Shell script

Exit status

By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not.

(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.

This value is know as Exit Status.

exit status can be found out by issuing $ echo $?

if the output is 0, the previously executed command is successful if it’s a non zero value means the last command run is unsuccessful.

 

Categories
Shell script

Quotes in scripts

Mainly three types of quotes are used.

1. ” – Double quotes: Anything enclose in double quotes removed meaning of that characters (except and $).

2. ‘ – Single quotes: Enclosed in single quotes remains unchanged.

3. ` – Back quotes: To execute command

 

 

Categories
Shell script

Shell Arithmetic

used to perform arithmetic operations in shell scripts.

Synatax: $ expr  op1 operator  op2

eg:

$ expr 1 + 3
$ expr 2 – 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 * 3
$ echo `expr 6 + 3`

in the last one we used back quote (` the one below the tilde symbol in standard keyboards).

if we use the back quote the last eg’s output may differ.

Categories
Shell script

Redirection in Shell-Unix/linux

Redirection is a function common to most command-line interpreters, including various unix shells that can redirect standard streams to user specified locations.

UNIX STANDARD I/O Streams

0- stdin (Standard input)

1- stdout (Standard output)

2- stderr (standard error)

 

 

 

Redirecting standard input and standard output

simple redirection to a file:  $command  > file

redirection &  appending to a file:   $command >> file

Redirecting to and from the standard file handles

eg: $ command  2> file

executes the command & redirects standard error to file.

Merging of standard error into standard output can be obtained by

eg: $ find / -name .profile > results 2>&1

 

Redirect to multiple outputs

The standard command “tee” can be used to redirect output from a command to several destinations

eg: $  ls -lrt | tee xyz

this redirects the file list output to both “standard output” and “file xyz”.