当前位置:网站首页>Shell Scripting

Shell Scripting

2022-07-06 15:59:00 Star age Cao Botao

Shell Programming

Once in a while Linux Write something simple in the environment Shell Script , This article can be used as a reference , Write some simple scripts .

Grammatical norms

Define the execution environment

#!/usr/bin/bash
 perhaps 
#!/usr/bin/env bash | python |perl

perform

 Modify the permissions 
chmod +x ./demo.sh
 perhaps 
chmod 700 ./demo.sh

./demo.sh
 perhaps 
bash ./demo.sh
sh ./demo.sh
#  adopt bash sh  There is no need to modify permissions , Specify the execution environment 

Special symbols

Symbol explain
~ Home directory
- Upper level directory
!! Execute the last order
$ Get environment variables ${USER}
±*/% Operator
* wildcard Multiple
One
| Pipe,
\ Escape character
> Redirect (echo abc > ./demo.txt)
>> Redirect (echo abc >> ./demo.txt) Additional
<wc < abc.txt
<<

notes

:<<EOF
 The comment ...
 The comment ...
 The comment ...
EOF

Variable

#  Defining variables 
name="zhangsan"
age=18
#  read-only 
readonly name
#  Delete variables 
unset age
#  visit 
echo ${name}
#  Get string length 
echo ${
    #name}
#  Extract substring 
echo ${string:0:2} #  Output  unoo

Array

arr=("a" "b" "c" "d")
echo arr[0]
#  Get all 
echo arr[@]
echo arr[*]
#  Gets the length of the array 
length=${
    #arr[@]}

operation

Native bash Simple mathematical operations are not supported , But you can do it with other commands , for example awk and expr,expr The most commonly used .

$(( Arithmetic expression )) or $[ Arithmetic expression ], More efficient .

#!/bin/bash

val=`expr 2 + 2`
echo " The sum of the two is  : $val"
# 1.(())
echo $((1+1))
# 2.let
let a+=6 c=a+b
#  Be careful , For similar let x+y This way of writing ,Shell  Although the calculation  x+y  Value , But discard the result ; If you don't want to , have access to let sum=x+y take  x+y  The result of is saved in the variable  sum  in .

read Read console input

Two points attention :

  • Space between expression and operator , for example 2+2 It is not right , Must be written as 2 + 2, This is different from most programming languages we are familiar with .

  • The complete expression is to be `` contain , Note that this character is not a common single quotation mark , stay Esc Key bottom .

  • Conditional expressions should be placed between square brackets , And there should be spaces , for example : [ a = = a== a==b] It's wrong. , Must be written as [ $a == $b ].

  • Multiplication sign (*) There must be a backslash in the front () To achieve multiplication ;

Operator explain give an example
! Non operation , Expression for true Then return to false, Otherwise return to true.[ ! false ] return true.
-o Or operations , There is an expression for true Then return to true.[ $a -lt 20 -o $b -gt 100 ] return true.
-a And operation , Both expressions are true To return to true.[ $a -lt 20 -a $b -gt 100 ] return false.

The file test operator is used to detect Unix Various attributes of the file .

The operator explain give an example
-b file Check if the file is a block device file , If it is , Then return to true.[ -b $file ] return false.
-c file Check whether the file is a character device file , If it is , Then return to true.[ -c $file ] return false.
-d file Check if the file is a directory , If it is , Then return to true.[ -d $file ] return false.
-f file Check if the file is a normal file ( It's not a catalog , It's not a device file ), If it is , Then return to true.[ -f $file ] return true.
-g file Check if the file is set SGID position , If it is , Then return to true.[ -g $file ] return false.
-k file Check whether the file is set with the adhesive bit (Sticky Bit), If it is , Then return to true.[ -k $file ] return false.
-p file Check if the file is a named pipeline , If it is , Then return to true.[ -p $file ] return false.
-u file Check if the file is set SUID position , If it is , Then return to true.[ -u $file ] return false.
-r file Check whether the file is readable , If it is , Then return to true.[ -r $file ] return true.
-w file Check whether the file is writable , If it is , Then return to true.[ -w $file ] return true.
-x file Check if the file is executable , If it is , Then return to true.[ -x $file ] return true.
-s file Check if the file is empty ( Is the file size greater than 0), Not empty return true.[ -s $file ] return true.
-e file Test files ( Including directory ) Whether there is , If it is , Then return to true.[ -e $file ] return true.

Other checkers :

  • -S: Determine whether a file is socket.
  • -L: Check whether the file exists and is a symbolic link .

Process control

if
if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi
for
for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

for((i=1;i<=5;i++))
do
    echo " This is the first  $i  Secondary call ";
done;
while
while condition
do
    command
done

#  Example 
#!/bin/bash
int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done

File contains

adopt . Import the files that need to be included

原网站

版权声明
本文为[Star age Cao Botao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919408037.html