Take the length of the variable
[[Email protected] scripts]# var=oldboy123[[email protected] scripts]# echo ${var}oldboy123[[email protected] scripts]# echo ${#var}9# For example: #以下判断用read输入一个值, if the length is 0, The value is empty, otherwise the value of the variable is printed [[email protected] scripts]# cat if4.sh #!/bin/bashread -p " pls input a num: " aif [ ${#a} -eq 0 ]then echo "a is null,pls input a nums again!" exit 1else echo "a= $a" fi# test [[email protected] scripts]# sh if4.sh pls input a num: a is null,pls input a nums again! [[email protected] scripts]# sh if4.sh pls input a num: da=d[[ Email protected] scripts]# sh if4.sh pls input a num: 123a=123
2. File name and directory interception
[Email protected] scripts]# test= "/etc/sysconfig/network-scripts/ifcfg-eth0" [[email protected] scripts]# echo ${test }/etc/sysconfig/network-scripts/ifcfg-eth0# file name [[email protected] scripts]# echo ${test##*/}ifcfg-eth0# fetch directory [email Protected] scripts]# echo ${test%/*}/etc/sysconfig/network-scripts# use commands basename and dirname can also achieve the same effect [email protected ] scripts]# echo ' basename $test ' ifcfg-eth0[[email protected] scripts]# echo ' dir $test '/etc/sysconfig/network-script S/ifcfg-eth0
Linux Shell String Common operations