Shell programming 7


人需要活得真实,放下面具,活出真实的自己。


SHELL=[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]

progress:[############## ] 70%


Ax Shell Substitution | 转义

Sr.No. Escape & Description
1 *\\* backslash |反斜杠
2 **\a **alert (BEL) | 警告
3 \b backspace | 空格
4 \c suppress trailing newline | 不换行
5 \f form feed | 换页
6 **\n **new line | 新的一行
7 **\r **carriage return | 回车
8 \t horizontal tab | 制表位
9 \v vertical tab | 垂直位

使用-E禁用反斜杠转义,使用-n禁用新行插入。

Command Substitution

转义为命令

`command`

#!/bin/sh

DATE=`date`
echo "Date is $DATE"

USERS=`who | wc -l`
echo "Logged in user are $USERS"

UP=`date ; uptime`
echo "Uptime is $UP"

执行脚本 −

Date is Thu Jul 2 03:59:57 MST 2009
Logged in user are 1
Uptime is Thu Jul 2 03:59:57 MST 2009
03:59:57 up 20 days, 14:03, 1 user, load avg: 0.13, 0.07, 0.15

Variable Substitution

Sr.No. Form Description
1 ${var} 替换var的值
2 ${var:-word} var未设置或为空,word将替换为var,var值不改变
3 ${var:=word} 如果var为空或未设置,var设置为word的值。
4 ${var:?message} 如果var为空或未设置,message将打印到标准错误。 这将检查变量是否设置正确。
5 ${var:+word} 如果设置了var,则word将替换为var。var的值不会改变。

#!/bin/sh

echo ${var:-"Variable is not set"}
echo "1 - Value of var is ${var}"

echo ${var:="Variable is not set"}
echo "2 - Value of var is ${var}"

unset var
echo ${var:+"This is default value"}
echo "3 - Value of var is $var"

var="Prefix"
echo ${var:+"This is default value"}
echo "4 - Value of var is $var"

echo ${var:?"Print this message"}
echo "5 - Value of var is ${var}"

执行脚本 −

Variable is not set
1 - Value of var is
Variable is not set
2 - Value of var is Variable is not set

3 - Value of var is
This is default value
4 - Value of var is Prefix
Prefix
5 - Value of var is Prefix

文章作者: Enomothem
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Enomothem !
  目录