我是靠谱客的博主 沉静御姐,这篇文章主要介绍linux shell 脚本命令操作案例大全,现在分享给大家,希望可以做个参考。

/*************************************************

author:郑金玮

time:2014/07/12

desc:impl shell command opt

*************************************************/


案例1:变量、函数、判断、比较

test.sh

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash echo ======================== this is a bash demo======================== printf "%s is %d years old n" zhengjinwei 23 declare count=10 declare fruit=apple declare strNormal="we have $count ${fruit}'s " declare strSpecial="we have $count ${fruit} " function printStr() { if [ $count -gt 1 ] ; then echo "$strNormal" else echo "$strSpecial" fi } printStr echo $(uname) ######################################### declare num=1000 function printNum() { local num=10 let num+=10 echo $num } echo "the global num is : $num " echo -e "call printNum Function :" printNum o "==================this is if else fi ....test demo=====================" if [ -f "test.sh" ] ; then echo "named test.sh is a file.." else echo "named test.sh is not a file.." fi function checkVariableHaveNum() { if [ -n "$1" ] ; then echo "this variable have value" else echo "this variable have not value" fi } declare g_num=10 checkVariableHaveNum $g_num function checkVariableEqual() { if [ "$1" -eq "$2" ] ; then printf "%d == %d " $1 $2 elif [ "$1" -ne "$2" ] ; then printf "%d != %d " $1 $2 elif [ "$1" -ge "$2" ] ; then printf "%d >= %d " $1 $2 elif [ "$1" -gt "$2" ] ; then printf "%d > %d" $1 $2 elif [ "$1" -lt "$2" ] ; then printf "%d < %d" $1 $2 else printf "%d <= %d" $1 $2 fi } checkVariableEqual 2 4 checkVariableEqual 4 2 checkVariableEqual 2 2


案例2:字符串操作(取子串)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
echo "-------------------------------------" ########this is string opt################################ #get string's length declare strName="zhengjinwei" declare strNameLength=${#strName} printf "the length of %s is %d n" $strName $strNameLength function getChildString() { local StrResourse=$1 local nParamCount=$2 local nStartPos=$3 local nEndPos=$4 if [[ $nParamCount -lt 3 ]] ; then echo "param less than 3 is err" elif [[ $nParamCount -gt 4 ]] ; then echo "param more than 4 is err" else if [[ $nParamCount -eq 3 ]] ; then let nEndPos=${#StrResourse} else let nEndPos=$4 fi fi local strResult=${StrResourse:$nStartPos:$nEndPos} printf "the child string in %s from position %d to %d is %s: nnn" $StrResourse $nStartPos $nEndPos $strResult } getChildString "zhengjinwei" 4 2 6 getChildString "zhengjinwei" 3 2


案例3:迭代、数组

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
########################################################### <span style="font-size:24px;"># this is array test demo declare array_int=(1 2 3 4 5 6 7) printf "the int value in array_int which index is %d is %dnn" 0 ${array_int["0"]} echo "=============for loop==============================" declare array_int_len=${#array_int[*]} for (( i=0 ;i<"$array_int_len";i++ )) do echo -e ${array_int[$i]} done echo "" echo "============for iterator loop================================" for value in ${array_int[@]} do echo -e $value done echo "" echo "==========while loop==================================" declare i=0 while (( $i<$array_int_len )) do echo -e ${array_int[$i]} let i=i+1 done ########################################################</span>


案例4:时间操作

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function getNowtime() { local _date=$(date +%Y-%m-%d:%H-%M-%S:%A) echo $_date } function getCostTime() { local _start=$(date +%S) sleep 2 local _end=$(date +%S) local _cost=$(( _end - _start )) echo $_cost } function getDiffTimeBetweenInSeconds() { local _start=$(date +%s -d '2010-09-01 17:00:00') local _end=$(date +%s -d '2011-09-01 18:40:40') tep=$(($_end-$_start)) tep2=$(( 3600*24 )) result=$(( $tep/$tep2 )) extra=$[ $tep%$tep2 ] declare _hours=0 declare _seconds=0 if [ $extra -ge 3600 ] ; then _hours=$(( $extra/3600 )) _seconds=$(( $extra%3600)) else _hours=0 _seconds=$extra fi printf "%d days ---%d hours ----%d seconds nn" $result $_hours $_seconds } getNowtime getCostTime getDiffTimeBetweenInSeconds


最后

以上就是沉静御姐最近收集整理的关于linux shell 脚本命令操作案例大全的全部内容,更多相关linux内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部