[WIP]Unix / Linux Shell Programming

Created: 2022/12/17

Finished: 2022/12/20

https://www.tutorialspoint.com/unix/shell_scripting.htm 

What is Shell?
   

A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output.

Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

 Shell Prompt    
$ // Bourne shell
% // C shell

The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command.

Shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.

 Shell Types  
Bourne shell

If you are using a Bourne-type shell, the $ character is the default prompt.

  • Bourne shell (sh)
  • Korn shell (ksh)
  • Bourne Again shell (bash)
  • POSIX shell (sh)
C shell

If you are using a C-type shell, the % character is the default prompt.

  • C shell (csh)
  • TENEX/TOPS C shell (tcsh)

 

 Shell Scripts

 suffix: .sh

Shell Comments #
shebang construct

 

#!/bin/sh

This tells the system that the commands that follow are to be executed by the Bourne shell. 

It's called a shebang because the # symbol is called a hash, and the ! symbol is called a bang.

 

 

 

   
   
Using Variables
   

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data.

A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.

Variable Names

The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).

By convention, Unix shell variables will have their names in UPPERCASE.

 
Defining Variables    
variable_name=variable_value

 

Accessing Values

 To access the value stored in a variable, prefix its name with the dollar sign ($) 

TEST=ture
echo $TEST

 

 

Read-only Variables 

Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be changed. 

TEST=true
readonly TEST

 

or

readonly TEST=true

 

 

Unsetting Variables   
unset variable_name

 

Variable Types
Local Variables A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.
Environment Variables An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.
Shell Variables A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.

 

   
Special Variables
   
$0 The filename of the current script.
$n These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
$! The process number of the last background command.
Command-Line Arguments   

The command-line arguments $1, $2, $3, ...$9 are positional parameters, with $0 pointing to the actual command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the command.

Following script uses various special variables related to the command line −

#!/bin/sh

echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

output: 

$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2

 

 

Special Parameters $* and $@   

There are special parameters that allow accessing all the command-line arguments at once. $* and $@ both will act the same unless they are enclosed in double quotes, "".

Both the parameters specify the command-line arguments. However, the "$*" special parameter takes the entire list as one argument with spaces between and the "$@" special parameter takes the entire list and separates it into separate arguments.

Exit Status  
   
   
Using Arrays
Defining Array Values 

  

array_name[index]=value
// bash
array_name=(value1 ... valuen) // starting with 1

 

 

 

Accessing Array Values

   

${array_name[index]}
// access all values
${array_name[*]}
${array_name[@]}
// or $array_name

 

 

   
Basic Operators
   Bourne shell didn't originally have any mechanism to perform simple arithmetic operations but it uses external programs, either awk or expr.
 Arithmetic Operators  
  • There must be spaces between operators and expressions. For example, 2+2 is not correct; it should be written as 2 + 2.

  • The complete expression should be enclosed between ‘ ‘, called the backtick.

  • It is very important to understand that all the conditional expressions should be inside square braces with spaces around them, for example  [ $a == $b ]  is correct whereas,  [$a==$b] is incorrect.

  • All the arithmetical calculations are done using long integers.

     
+ (Addition)

Adds values on either side of the operator

 

`expr $a + $b`

 

 

- (Subtraction)

Subtracts right hand operand from left hand operand

`expr $a - $b`

 

 

* (Multiplication)

Multiplies values on either side of the operator

`expr $a * $b`

 

 

/ (Division)

Divides left hand operand by right hand operand

`expr $a / $b`

 

 

% (Modulus)

Divides left hand operand by right hand operand and returns remainder

`expr $a % $b`

 

 

= (Assignment)

Assigns right operand in left operand

a=$b 

 

 

== (Equality) 

Compares two numbers, if both are same then returns true.

[ $a == $b ]

 

!= (Not Equality)

 Compares two numbers, if both are different then returns true.

[ $a != $b ] 

 

 

   
   
Relational Operators 

 Bourne Shell supports the following relational operators that are specific to numeric values. These operators do not work for string values unless their value is numeric.

-eq

Checks if the value of two operands are equal or not; if yes, then the condition becomes true.

[ $a -eq $b ]

 

 

-ne

 Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true.

[ $a -ne $b ] 

 

 

-gt 

 Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true.

[ $a -gt $b ]

 

 

-lt 

Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true.

[ $a -lt $b ]

 

 

-ge

Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. 

[ $a -ge $b ]

 

 

-le 

 Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true.

[ $a -le $b ]

 

 

 

 

Boolean Operators

 

!

This is logical negation. This inverts a true condition into false and vice versa.

[ !false ]

 

-o

 This is logical OR. If one of the operands is true, then the condition becomes true.

[ $a -lt 20 -o $b -gt 100 ]

 

 

-a

 This is logical AND. If both the operands are true, then the condition becomes true otherwise false.

[ $a -lt 20 -a $b -gt 100 ]

 

 

 

String Operators   
=

Checks if the value of two operands are equal or not; if yes, then the condition becomes true.

[ $a = $b ]

 

 

 !=

 Checks if the value of two operands are equal or not; if values are not equal then the condition becomes true.

[ $a != $b ]

 

 

-z

 Checks if the given string operand size is zero; if it is zero length, then it returns true.

[ -z $a ] 

 

 

-n

 Checks if the given string operand size is non-zero; if it is nonzero length, then it returns true.

[ -n $a ]

 

 

 str

 Checks if str is not the empty string; if it is empty, then it returns false.

[ $a ]

 

 

 

 

 File Test Operators  TODO 
Decision Making
The if...else statements

 

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

If expression is a shell command, then it will be assumed true if it returns 0 after execution. If it is a Boolean expression, then it would be true if it returns true.

 

 

 

case word in
   pattern1)
      Statement(s) to be executed if pattern1 matches
      ;;
   pattern2)
      Statement(s) to be executed if pattern2 matches
      ;;
   pattern3)
      Statement(s) to be executed if pattern3 matches
      ;;
   *)
     Default condition to be executed
     ;;
esac

 

 

   
   
   
   
Shell Loops
The while Loop

 

while command
do
   Statement(s) to be executed if command is true
done

 

 

The for Loop 

 

for var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done

 

 

a=(9 8 7 6 5 4 3 2 1)
for i in ${a[*]}; do
  echo "i=$i"
done

 

 

 The until Loop

 

until command
do
   Statement(s) to be executed until command is true
done

 

 

 The select Loop

 

select var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done

 

 

The break statement   The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop.
The continue statement   The continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop.
Shell Substitutions
  The shell performs substitution when it encounters an expression that contains one or more special characters. 
 The Escape sequences   
\\ \
\a alert (BEL)
\b backspace
\c suppress trailing newline
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
Command Substitution 

 

`command`

 

 

Variable Substitution   
 ${var}  Substitute the value of var.
 ${var:-word}  If var is null or unset, word is substituted for var. The value of var does not change.
 ${var:=word}  If var is null or unset, var is set to the value of word.
 ${var:?message}  If var is null or unset, message is printed to standard error. This checks that variables are set correctly.
 ${var:+word}  If var is set, word is substituted for var. The value of var does not change.
   
   
Quoting Mechanisms
 

The shell performs substitution when it encounters an expression that contains one or more special characters. 

* ? [ ] ' " \ $ ; & ( ) | ^ < > new-line space tab

 

 

 
Single quote All special characters between these quotes lose their special meaning.
Double quote

Most special characters between these quotes lose their special meaning with these exceptions −

  • $
  • `
  • \$
  • \'
  • \"
  • \\
Backslash  Any character immediately following the backslash loses its special meaning.
Back quote 

Anything in between back quotes would be treated as a command and would be executed.

a=`command` 

 

 

 

   
   
   
   
IO Redirections
 Output Redirection

 

command > file

 

 

Input Redirection 

 

command < file

 

 

Here Document 

 A here document is used to redirect input into an interactive shell script or program.

command << delimiter
document
delimiter

 

 

Discard the output

 

$ command > /dev/null

 

 discard both STDOUT and STDERR

$ command > /dev/null 2>&1

 

 

pgm > file Output of pgm is redirected to file
pgm < file Program pgm reads its input from file
pgm >> file Output of pgm is appended to file
n > file Output from stream with descriptor n redirected to file
n >> file Output from stream with descriptor n appended to file
n >& m Merges output from stream n with stream m
n <& m Merges input from stream n with stream m
<< tag Standard input comes from here through next tag at the start of line
|

Takes output from one program, or process, and sends it to another

a | b | c

0: STDIN 1: STDOUT 2: STDERR

 

   
Shell Functions
 Creating Functions

  

function_name () { 
   list of commands
}

 

 

Pass Parameters to a Function   You can define a function that will accept parameters while calling the function. These parameters would be represented by $1, $2 and so on.
Returning Values from Functions 

 If you execute an exit command from inside a function, its effect is not only to terminate execution of the function but also of the shell program that called the function.

Based on the situation you can return any value from your function using the return command whose syntax is as follows −

return anything

 

Here code can be anything you choose here, but obviously you should choose something that is meaningful or useful in the context of your script as a whole.

Nested Functions  One of the more interesting features of functions is that they can call themselves and also other functions. A function that calls itself is known as a recursive function.
Function Call from Prompt 
  • You can put definitions for commonly used functions inside your .profile. These definitions will be available whenever you log in and you can use them at the command prompt.
   
Manpage Help
 

 

$man command

 

 

   
posted @ 2022-12-17 15:51  懒虫哥哥  阅读(32)  评论(0编辑  收藏  举报