Quick Start basic shell programming syntax of linux shell

  From a programmer's point of view, Shell itself is a program written in C language, from the user's point of view, Shell is the user and the Linux operating system a bridge. Users can either enter the command execution, and can use Shell Scripting, complete more complex operations. In the Linux GUI increasingly sophisticated today, in the areas of systems management, Shell program still plays a significant role. Depth understanding and proficiency Shell programming is one of the compulsory homework every Linux user.

  Many Linux-Shell type, common are: Bourne Shell (/ usr / bin / sh or / bin / sh), Bourne Again Shell (/ bin / bash), C Shell (/ usr / bin / csh), K Shell ( / usr / bin / ksh), Shell for Root (/ sbin / sh), and the like. Shell syntax of different languages ​​are different, it can not be used interchangeably. Each place has its own characteristics Shell, basically, to master any of them enough. In this article, our focus is Bash, is the Bourne Again Shell, due to the ease of use and free, Bash is widely used in daily work; at the same time, Bash is the default most Linux systems Shell. In general, people do not distinguish between Bourne Shell and Bourne Again Shell, therefore, in the following text, we can see that #! / Bin / sh, it can also be changed to #! / Bin / bash.

  A basic grammar

  1. variable: export global variables

  #! / bin / bash 

  num = $ num 1export 

  echo $ num

 

  2. Analyzing conditions

 

 if [[ condition ]]; then

  #statements

  if

  #!/bin/bashif [[ $1 == 1 ]]; then

  echo $1

  else

  echo "value is not 1"

  if

 

  Analyzing common parameter list

  

 

  3.case branch

  #!/bin/bash

  case $1 in

 

  1)

  echo "the value is 1";;

 

  2)

  echo "the value is 2";;

 

  3)

  echo "the value is 3";;

 

  4)

  echo "the value is 4";;

  *)

  echo "defult value"

  esac

 

  4. The for loop

  #方法1for (( i = 0; i < 10; i++ )); do

  #statements

  done#方法2for i in words; do #statementsdone
  #方法1for (( i = 0; i < 10; i++ )); do

  #statements

  done#方法2for i in words; do #statementsdone

 

  Routine:

  #!/bin/bash

  for (( i = 0; i < 10; i++ )); do

  echo "the value is $i"

  done

 

  5.while cycle

  while [[ condition ]]; do

  #statements

  done

 

  Routine:

  #!/bin/bash

  i=$1

  while [[ $i != 0 ]];

  do

  echo "value is $i"

  let "i=i-1"

  done

 

  6, function

  1, must be in place before calling the function, declare the function, shell script is run line by line. Not like other languages, like the first pre-compiled. One must declare the function before using the function.

  2, total = $ (fSum 3 2); call this method, we clearly know that the shell in single brackets, can be: a command statement. Therefore, we can function in the shell, seen as the definition of a new command, it is a command, so each input parameter directly separated by a space. A command which is obtained by the method parameters: $ 0 ... $ n obtained. $ 0 for the function itself.

  3, the function returns a value, only through the $? Variable system obtained by direct = get a null value. In fact, we have an understanding in accordance with the above, know the function is a command, the command shell to get the return value goes through the $? Obtain.

  #!/bin/bash

  function add()

  {

  echo $1 $2

  return $(($1+$2))

  }

  value=$(add $1 $2);

  echo $value,$?

 

  7. array

 

 # Number of elements in the array element 

  length = $ {# ARRAY_NAME [@]} 

  # or 

  length = $ {# ARRAY_NAME [* ]} 

  # length of the array of individual elements made 

  lengthn = ARRAY_NAME $ {# [n-]}

 

  The general format of the read array element value is:

  

${array_name[index]}valuen=${array_name[2]}

 

  Routine

 

 #!/bin/bash

  name=(a b c d e f g)

  length=${#name[@]}

  echo $length

  for (( i = 0; i < $length; i++ )); do

  echo valuen=${name[$i]}

  done

Share information about entry-fast programming shell programming

http://www.makeru.com.cn/live/3485_1489.html?s=45051

Guess you like

Origin www.cnblogs.com/923327iu/p/11841422.html