练习1:将/etc/passwd中包含root的那一行数据保存到变量中
提示:grep"root"/etc/passwd
bash
#!/bin/bash
var=$(grep "root" /etc/passwd)
echo var=$var
练习2:字符串截取
var=http:/www.baidu.com/index.html/cn
2.要求获取baidu
3.要求获取cn
bash
#!/bin/bash
var=http://www.baidu.com/index.html/cn
str1=${var:7:13}
echo "str1=$str1"
str2=${var:11:5}
echo "str2=$str2"
str3=${var:0-2:2}
echo "str3=$str3"
str4=${var#*//}
str4=${str4%%/*}
echo "str4=$str4"
str5=${var#*.}
str5=${str5%%.*}
echo "str5=$str5"
str6=${var##*/}
echo "str6=$str6"