[WIP]Unix / Linux Shell Programming

Created: 2022/12/17

 

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
   
   
   
   
   
   
Basic Operators
   
   
   
   
   
   
Decision Making
   
   
   
   
   
   
Shell Loops
   
   
   
   
   
   
Shell Substitutions
   
   
   
   
   
   
Quoting Mechanisms
   
   
   
   
   
   
IO Redirections
   
   
   
   
   
   
Shell Functions
   
   
   
   
   
   
Manpage Help
   
   
   
   
   
   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章