linux bash of basic grammar

Examples are from the network, it comes with the original link address, finishing issued their own practice, all available test

linux shell basic grammar - Zhouxue Wei - blog Park
https://www.cnblogs.com/zxouxuewei/p/6393092.html

Variable declaration and assignment

Define variables equal sign about no spaces

By export become global variables

#!/bin/bash
#a.sh
a=666
b=hello
g_user=root
export g_user#全局变量
echo $a
echo $b
echo $g_user

#调用 
bash a.sh

The command into a variable

  1. Parentheses
current_date=$(date)
echo current data is [$current_date]
  1. Backticks, and tildes a key on the keyboard (reference only, can use 1)
current_date=`date`
echo current data is [$current_date]
  1. Braces plus anti-quotation marks (for reference only, you can use 1)
current_date={`date`}
echo current data is [$current_date]

if condition is determined

#!/bin/bash
#a.sh
if [[ $1 == 1 ]]; then
    echo "first argument is 1"
else
    echo "first argument is not 1"
fi

#调用 
bash a.sh 1

Analyzing case branch

#!/bin/bash
#a.sh
case $1 in
    1)
        echo "the case is 1"
    ;;
    2)
        echo "the case is 2"
    ;;
    3)
        echo "the case is 3"
    ;;
    4)
        echo "the case is 4"
    ;;
    *)
        echo "no case match first argument"
    ;;
esac

#调用 
bash a.sh 2

for loop

In the form of a

#!/bin/bash
#a.sh
for (( i = 0; i < 10; i++ )); do
    echo "current value is $i"
done

#调用 
bash a.sh

Form 2

#!/bin/bash
#a.sh
for i in 1 2 3 4 5; do
    echo "current value is $i"
done

#调用 
bash a.sh

while loop

#!/bin/bash
#a.sh
i=$1

while [[ $i > 0 ]]; 
do
    echo "now i's value is $i"
    let "i=i-1"
done

#调用 
bash a.sh 10

function

shell-defined functions - determined to make a good programmer - blog Park
https://www.cnblogs.com/oxspirt/p/7246075.html

  • 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); by calling this method, we clearly know that the shell in single brackets, can be a :command statement. Thus, we can function in the shell, seen as a new command is defined, it is a command, and therefore various parameters separated by a space directly, the command which is obtained by the method parameters: \ (0 ... \) n-obtained. $ 0 for the function itself.
  • 3, the function return values, only by $?obtaining system variable, directly =obtaining 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, we need to $?get.
  • 4. The belt can function fun () is defined, may be directly fun () is defined, without any parameters.
  • The return parameters, may be displayed add: return returned, if not, will last a result of that command, as a return value. followed by the return value of n (0-255)
#!/bin/bash
#a.sh
function add()
{
    echo "arg1 is $1,arg2 is $2"
    return $(($1+$2))
}

plus()
{
    echo "arg1 is $1,arg2 is $2"
    return $(($1*$2))
}

value=$(add $1 $2);
echo "[echo value:$value,function add value:$?]"

value=$(plus $1 $2);
echo "[echo value:$value,function plus value:$?]"

#调用 
bash a.sh 5 7

Array

#!/bin/bash
#a.sh
name=(a bbb c d e f g) #数组
length=${#name[@]} #获取数组长度

echo length1:${#name[@]} #获取数组长度
echo length2:${#name[*]} #获取数组长度
echo length3:${#name[1]} #取得数组单个元素的长度

for (( i = 0; i < $length; i++ )); do
    echo value $i=${name[$i]} #读取数组的值
done

#调用 
bash a.sh

I / O

_Linux shell_ depth explanation of Shell script redirect home
https://www.jb51.net/article/142918.htm

>  输出重定向,覆盖方式
>> 输出重定向,追加方式
<  输入重定向
<< 标准输入来自命令行的一对分隔号的中间内容

Examples

#生成 input.txt
cat <<EOF>input.txt
zhao
qian
sun
li
zhou
wu
zheng
wang
EOF

#将input.txt文件作为sort命令的输入
sort < input.txt

# 将排序后的结果输出到input.sort.txt文件中
sort < input.txt >input.sort.txt

# 将排序后的结果追加输出到input.txt文件中
sort < input.txt >> input.txt

#将标准输入重定向为input.txt文件,标准输出重定向为input2.txt文件(追加),将标准错误重定向为error文件
sort 0< input.txt 1>> input2.txt 2> input.error.txt

Input program may come from a keyboard , may be from a file or other device ; Similarly, a program may be displayed on the output screen or saved to a file. This involves the standard input , standard output and standard error .

Input program is standard input, the default keyboard, the user can specify a file or other device.

There are two output program, i.e., the standard output and standard error, which is the normal output standard output program, a standard error of the output of the program error. Both have been designated as the default screen, the user can specify a file or other device.

Input and output to each source has a descriptor standard input descriptors 0 and standard output as a descriptor, the descriptor is the standard error 2.

EOF

EOF written under linux comb - cleared flashy - blog Park
https://www.cnblogs.com/kevingrace/p/6257490.html

Script execution time, the need to automatically input N line to the contents of a file. If a minority of a few lines, you can use echo append mode, but if it is many lines, then simply using echo additional ways it is very stupid!
This time, you can use in conjunction with EOF cat command to append the contents of the line.

EOF is END Of File Abbreviation, represents a custom terminator. Since custom, then EOF is not fixed , can freely set the alias, the linux press ctrl-d represents EOF.
EOF generally with cat can be multiple lines of text output .

#把EOF的内容作为cat 的输入
cat <<EOF
aaa
bbb
ccc
EOF

#把EOF的内容作为cat 的输入,并把cat的输出定向到a.txt
cat <<EOF > a.txt
aaa
bbb
ccc
EOF

getopts

For parameter definitions

#!/bin/bash
#a.sh

while getopts "a:b:c:r:" arg; do
   case $arg in 
    a)
        a=$OPTARG
    ;;
    b)
        b=$OPTARG
    ;;
    c) 
        c=$OPTARG
    ;;
    r)
        repo=$OPTARG
    ;; 
    ?)
        echo you need help
    ;;
   esac
done

echo "a:$a,b:$b,c:$c,repo:$repo"

#调用 
bash a.sh -a 3 -b 4 -c 5 -r 6

Guess you like

Origin www.cnblogs.com/DHclly/p/11388310.html