シェルLinuxスクリプト解析の例



Examples Shell Linux Script Parsing



例1-ex1.sh:

#!/bin/sh #This is to show what a example looks like. echo 'My First Shell!' echo 'This is current directory.' /bin/pwd echo echo 'This is files.' /bin/ls

結果:

My first shell! This is current directroy. /test This is files. a.sh a.tar.gz a.zip ex1.sh ex2.info ex2.sh ex3.sh shelldir

例2-ex2.sh:

#!/bin/sh /bin/date +%F >> /test/shelldir/ex2.info echo 'disk info:' >> /test/shelldir/ex2.info /bin/df -h >> /test/shelldir/ex2.info echo >> /test/shelldir/ex2.info echo 'online users:' >> /test/shelldir/ex2.info /usr/bin/who | /bin/grep -v root >> /test/shelldir/ex2.info echo 'memory info:' >> /test/shelldir/ex2.info /usr/bin/free -m >> /test/shelldir/ex2.info echo >> /test/shelldir/ex2.info #write root /usr/bin/write root

結果:
上記のすべての情報がタイミング/test/shelldir/ex2.infoに出力され、ルートに送信されることを意味します



例3-ex3.sh

#!/bin/sh DATE = `/ bin / date +% Y% m% d`` `as a command character replacement echo 'TODAY IS $DATE' / Bin / ls -l $ 1 $ 1 $ 2 $ 3 is a placeholder, i.e. when the command to execute the transmitted parameters /bin/ls -l $2 /bin/ls -l $3

実装の結果:sh ex3.sh / usr / local / usr / local / shelldir / usr / local / redis

TODY IS 20180727 total 52 drwxr-xr-x. 2 root root 4096 Jul 26 23:28 bin drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games drwxr-xr-x. 2 root root 4096 Sep 23 2011 include drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64 drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec drwxr-xr-x. 4 root root 4096 Jul 26 23:31 redis drwxrwxr-x. 6 root root 4096 Jan 13 2015 redis-3.0.3 drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin drwxr-xr-x. 5 root root 4096 Jul 25 04:14 share drwxr-xr-x. 2 root root 4096 Jul 26 23:23 software drwxr-xr-x. 2 root root 4096 Sep 23 2011 src

例4-ex4.sh:

特別な変数:
$ *このプロセスのすべてのパラメータ
パラメータの数$#このプログラム
プログラムの$$ PID
$!バックグラウンドを実行するPIDコマンド
$?コマンドの戻り値を実行する
$(0〜9)いくつかのパラメーターの出力値



#!/bin/sh DATE=`/bin/date +%F` echo 'today is $DATE' echo '$# :' $# echo '$* :' $* echo '$? :' $? echo '$$ :' $$ echo '$0 :' $0

結果:

today is 2018-07-27 $#:0 $*: $?:0 $$:28805 $0:ex4.sh

例5-ex5.sh:

readコマンドは、変数に割り当てられたキーボードからデータを読み取ります。

#!/bin/sh read f s t echo 'the first is $f' echo 'the second is $s' echo 'the third is $t'

-Xは、実行の実装の進行状況を追跡するためのパラメーターを追加できます



例6-ex6.sh:

整数のみのexpr算術+-* /
注意:
1、開口部の間にスペースがあるexpr比較操作
2、*は乗算エスケープ文字を示します
3、加算演算子の罰の後に最初のカウントを保持します。操作優先コマンド置換文字を追加する必要がある場合
4、算術演算は変数に対して実行できます

テストコマンド:コマンドは、一般に制御ステートメントと組み合わせて使用​​されるテストファイル、文字列などを使用してテストできます。結果が表示されない場合は、単独でテストできます。

ifステートメントの構文:
test -d $ 1の場合、…else…
testは[]の場所に置くことができます。たとえば、test -d $ 1は['space'-d'space' $ 1]と同等です。

#!/bin/sh # if test $1 then ... else ... fi if [-d $ 1] test -d test whether the directory $! placeholders then echo 'this is a directory!' else echo 'this is not a directory!' fi

例7-ex7.sh

#!/bin/sh # if test then ... elif test then ... else ... fi if [ -d $1 ] then echo 'is a directory!' elif [ -f $1 ] then echo 'is a file!' else echo 'error!' fi

例8-ex8.sh

#!/bin/sh # -a -o if [ $1 -eq $2 -a $1 = 1 ] then echo 'param1 == param2 and param1 = 1' elif [ $1 -ne $2 -o $1 = 2 ] then echo 'param1 != param2 or param1 = 2' else echo 'others' fi

-a-o論理積または論理積

例9-ex9.sh

#!/bin/sh # for var in [params] do ... done for var in 1 2 3 4 5 6 7 8 9 10 do echo 'number is $var' done

例10-ex10.sh

#!/bin/sh # select var in [params] do ... done select var in 'java' 'c++' 'php' 'linux' 'python' 'ruby' 'c#' do break done echo 'you selected $var'

例11-ex11.sh

#!/bin/sh read op case $op in a) echo 'you selected a' b) echo 'you selected b' c) echo 'you selected c' *) echo 'error' esac

例12-ex12.sh

#!/bin/sh #while test do ... done num=1 sum=0 while [ $num -le 100 ] do sum=`expr $sum + $num` num=`expr $num + 1` done #sleep 5 echo $sum

例13-ex13.sh

#!/bin/sh i=0 while [ $i -le 100 ] do i=`expr $i + 1` if [ $i -eq 5 -o $i -eq 10 ] then continue else echo 'this number is $i' fi if [ $i -eq 15 ] then break fi done