$ cat test2.sh #!/bin/bash function f(){ echo "hello world" } f #函数调用 f #函数连续调用两次 # 输出: hello world hello world
$ a="hello" $ b="world" $ echo $a$b helloworld $ echo $a $b # $a $b中间有空格 hello world $ c=$a$b $ echo $c helloworld $ d=$a $b #不能有空格 -sh: world: command not found
a=5 b=6 $ expr $a + $b 11 $ echo $((a + b)) 11 $ echo $[a + b] 11 echo $(($a+$b)) # echo $((a+b)),双小括号里面可以不用加$符号 echo $[$a+$b] # echo $[a + b],可以不加$符号,可以没有空格 $ expr $a+$b 5+6
#!/bin/bash if [ -f "/home/qiuxianyin/mytest/test.sh" ] #紧挨着 [ ] 左右有空格,否则报错 then echo "file exists" fi if
参数 [ -f file ] 如果file存在且是一个普通文件则为真
这一行说明要使用的 shell。#!/bin/bash 表示脚本使用 /bin/bash。
head -10 file|tail -1 // head -n10ͬhead-10
[ -z "" ] && echo 0 || echo 1 0 [ -z "hi" ] && echo 0 || echo 1 1
[ -z STRING ] 判断字符串为空,“STRING” 的长度为零则为真。
使脚本所有者拥有读和可执行权限。
name=John && echo 'My name is $name' My name is $name #题中是单引号
’ - 当我们不希望把变量转换为值的时候使用它。
” - 会计算所有变量的值并用值代替。
文章来源: [Shell习题] 常见题系列二