Shell脚本命令
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | #!/bin/bash next_tips() #$0代表当前文件名 $1第一个参数 $2 第2个参数 .... { echo "---------$1-------------------" } next_tips "global or local varialbe test" #变量赋值左右两边不能有空格 var_test() { # local 局部变量 echo "global v1:$v1" local v1= "456" echo "local v1:$v1" } v1= "123" var_test echo "global2 v1:" ${v1} next_tips "external varialbe test" # $0 脚本文件名称 $1 第一个参数 $2 第二个参数 # $# 参数个数 # $? 前一个命令或函数返回的状态码 # $* 将所有参数通过字符串返回 # $$ 返回本进程PID external_fun() { #$0 $#(参数个数) $?(上个命令状态码) $*(打印所用参数) echo "parms total:$#" echo "func name:$0" test 1 - eq 2 echo "the return code:$?" test 1 - ne 2 echo "the return code:$?" echo "all parms is:$*" echo "the pid is :$$" } external_fun a b c d e f g next_tips "system vaiable test" # PATH HOME LOGNAME SHELL PWD system_var_func() { # PATH HOME LOGNAME SHELL PWD echo "PATH is:$PATH" echo "HOME is:$HOME" echo "LOGNAME is:$LOGNAME" echo "SHELL is:$SHELL" echo "PWD is:$PWD" } system_var_func next_tips "vaiable operate test" # "${}" 可输出值 # '${}' 不可输出值,继续打印${} # `pwd` 反引号,推荐使用$() # \ 注释部分 specail_func() { var1= "binfire" echo "binfire var:${var1}" echo 'binfire var:${var1}' echo ` pwd ` echo $( pwd ) echo "hello,\"binfire\"" } specail_func next_tips "condition string test" # 检测测试表达式,如果成立返回0,否则返回非0 # 检测表达式有两种方式 test 或 [] 必须要用空格分开 # 字符串 = != -z (空字符串) -n (非空字符串) # 数值 -eq -ne -gt -lt -ge -le # 文件 -e -d (目录) -f(常规文件) -w -x -r -L(链接文件) -u (设置了suid) # 逻辑运算符 ! -a (all) -o (or) condition_string_test() { # 字符串 = != -z (空字符串) -n (非空字符串) a= "11" b= "11" test "$a" = "$b" echo "a=b:$?" test "$a" != "$b" echo "a!=b:$?" test -z $a echo "a为空:$?" test -n $a echo "a不为空:$?" } condition_string_test next_tips "condition int test" condition_int_test() { # 数值 -eq -ne -gt -lt -ge -le a=1 b=1 test "$a" - eq "$b" echo "a=b:$?" test "$a" - ne "$b" echo "a!=b:$?" test "$a" -lt "$b" echo "a<b:$?" test "$a" -gt "$b" echo "a>b:$?" test "$a" - le "$b" echo "a<=b:$?" test "$a" - ge "$b" echo "a>=b:$?" } condition_int_test next_tips "condition file test" condition_file_test() { # 文件 -e -d (目录) -f(常规文件) -w -x -r -L(链接文件) -u (设置了suid) test -e /e/expr/shell/var .sh echo "file exists:$?" test -d /e/expr/shell echo "dir exists:$?" test -f /e/expr/shell/var .sh echo "file attr-normal:$?" test -w /e/expr/shell/var .sh echo "file attr-w:$?" test -x /e/expr/shell/var .sh echo "file attr-x:$?" test -r /e/expr/shell/var .sh echo "file attr-r:$?" test -L /e/expr/shell/var .sh echo "file attr-L:$?" test -u /e/expr/shell/var .sh echo "file attr-u:$?" } condition_file_test next_tips " -a -o ! test " condition_and_or_test() { # -a (and) -o (or) test 3 - eq 3 -a 4 - eq 4 echo "and test:$?" test 3 - eq 4 -o 4 - eq 4 echo "or test:$?" test ! 3 - eq 3 echo "! test:$?" } condition_and_or_test next_tips " if xxx; then elif else if xxx then; fi" ifelse_test() { #请输入个分数;分数必须>=0或<=100,如果输入错误,重新输入; A>=90 B >=80 C>=70 D>=60 E<60 echo "please input a score need ge 0 and le 100" read score while test "$score" -lt 0 -o "$score" -gt 100 do echo "input error,please input a score need ge 0 and le 100" read score done if test "$score" - ge 90; then echo "A" elif test "$score" - ge 80; then echo "B" elif test "$score" - ge 70; then echo "C" elif test "$score" - ge 60; then echo "D" else echo "E" fi } ifelse_test next_tips " exit 状态码测试" exit_test() { echo "hello,world" echo "$?" #exit 100 #返回码<255,外部接收时候即返回100 } exit_test echo "$?" next_tips "case in ) esac" case_test() { read num case $num in 1) echo "1" ;; 2) echo "2" ;; 3) echo "3" ;; esac } case_test next_tips "int operate test" int_test() { a=1 b=2 let c=a+b echo "c=$c" let c=c-a echo "c=$c" let c=c*a echo "c=$c" let c=c /a echo "c=$c" let c=c**2 #2次方 echo "c=$c" let c=c%a echo "c=$c" } int_test next_tips " for while brek continue" forwhilebreakcontinue_test() { for ((i=1;i<=5;i++)) #带个双括号 do echo $i done #------ for file in $( ls ) do echo $ file done #------ i=1 while test $i -lt 5 do echo "$i" if test $i - eq 3 ; then echo "fk will break" break fi if test $i - eq 1 ; then echo "fk will continue" let i=i+1 echo "now i:$i" continue fi let i=i+1 done } forwhilebreakcontinue_test read |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix