#shell字符串取子串:${file:0:5}:提取最左边的 5 个字节:/dir1${file:5:5}:提取第 5 个字节右边的连续 5 个字节:/dir2#shell字符串变量值的替换:${file/dir/path}:将第一个 dir 提换为 path:/path1/dir2/dir3/my.file.txt${file//dir/path}:将全部 dir 提换为 path:/path1/path2/path3/my.file.txt#${}还可针对变量的不同状态(没设定、空值、非空值)进行赋值:${file-my.file.txt} :假如 $file 没有设定,则使用 my.file.txt 作传回值 。(空值及非空值时不作处理) ${file:-my.file.txt} :假如 $file 没有设定或为空值,则使用 my.file.txt 作传回值 。(非空值时不作处理)${file+my.file.txt} :假如 $file 设为空值或非空值,均使用 my.file.txt 作传回值 。(没设定时不作处理)${file:+my.file.txt} :若 $file 为非空值,则使用 my.file.txt 作传回值 。(没设定及空值时不作处理)${file=my.file.txt} :若 $file 没设定,则使用 my.file.txt 作传回值,同时将 $file 赋值为 my.file.txt。(空值及非空值时不作处理)${file:=my.file.txt} :若 $file 没设定或为空值,则使用 my.file.txt 作传回值,同时将 $file 赋值为 my.file.txt。(非空值时不作处理)${file?my.file.txt} :若 $file 没设定,则将 my.file.txt 输出至 STDERR 。(空值及非空值时不作处理)${file:?my.file.txt} :若 $file 没设定或为空值,则将 my.file.txt 输出至 STDERR 。(非空值时不作处理)tips:以上的理解在于, 你一定要分清楚 unset 与 null 及 non-null 这三种赋值状态.一般而言, : 与 null 有关, 若不带 : 的话, null 不受影响, 若带 : 则连 null 也受影响.
#计算shell字符串变量的长度:${#var}${#var} 可计算出变量值的长度:${#file} 可得到 27 ,因为 /dir1/dir2/dir3/my.file.txt 刚好是 27 个字节...#bash数组(array)的处理方法数组:A=(a b c d)引用数组:${A[@]}${A[*]}访问数组成员${A[0]}计算数组长度${#A[@]}${#A[*]}数组重新赋值A[2]=xyz# $(( ))是用来做整数运算的 a=5;b=7;c=2;echo $(( a + b * c))
9.与* 区别在哪?
- "$@" 则可得到 "p1" "p2 p3" "p4" 这三个不同的词段
- "$*" 则可得到 "p1 p2 p3 p4" 这一整串单一的词段
10. && 与 || 差在哪?
- test命令有两种形式
- test expression
- [ expression ]
- bash的test目前支持三种测试对象
- string:字符串
- integer:整数
- file:文件
- 当expression为真是返回 0(true) ,否则返回 非0(false)
- command1 && command2 # command2只有在command1的RV为0(true)的条件下执行 。
- command1 || command2 # command2只有在command1的RV为非0(false)的条件下执行 。
- 先替换变量再比较
A=123[ -n "$A" ] && ([ "$A" -lt 100 ] || echo "too big")unset A
11. > 与 < 差在哪?
- 0: Standard Input (STDIN)
- 1: Standard Output (STDOUT)
- 2: Standard Error Output (STDERR)
我们可用> 来改变送出的数据信道(stdout, stderr),使之输出到指定的档案 。
ls my.file no.such.file 1> file.out 2>file.err# 2>&1 就是将stderr并进stdout做输出ls my.file no.such.file 1> file.out 2>&1# /dev/null 空ls my.file no.such.file >/dev/null 2>&1cat < file > file# 在 IO Redirection 中,stdout 与 stderr 的管道会先准备好,才会从 stdin 读进资料 。# 也就是说,在上例中,> file 会先将 file 清空,然后才读进 < file , # 但这时候档案已经被清空了,因此就变成读不进任何数据了
12. 你要if还是case呢?
# ifecho -n "Do you want to continue?(Yes/No):"read YNif [ "$YN"=Y -o "$YN"=y -o "$YN"="Yes" -o "$YN"="yes" -o "$YN"="YES"];thenecho "continue"elseexit 0fi# caseecho -n "Do you want to continue?(Yes/No):"read YNcase "$YN" in[Yy]|[Yy][Ee][Ss])echo "continue";;*)exit 0esac
13. for what? while与until差在哪?
# forfor ((i=1;i<=10;i++))do echo "num is $i"done# whilenum=1while [ "$num" -le 10 ]; do echo "num is $num" num=$(($num + 1))done# untilnum=1until [ "$num" -gt 10 ]; do echo "num is $num" num=$(($nu + 1))done
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- python 一键生成对账单
- 破冰行动中的蔡队到底是什么人,破冰行动中蔡军是谁-
- 琅琊榜林殊为什么叫太皇太后太奶奶,琅琊榜中太奶奶是小殊什么人-
- 黄曲霉素主要存在于什么食物中?
- 抖音活跃度在哪里看?
- 古代兵法对中医影响的研究
- 翡翠|翡翠四大挂件,其中的含义要清楚,选择适合自己的挂件
- 银元|中国最值钱的钱币,最贵的一枚价值2000余万
- 中国电信|女儿为已故父亲按时交话费被停机引热议 对该行为不满:电信回应难服众
- 中年男人都是孤独的,中年男人时常会觉得孤独-