环境变量
环境变量的传递问题
#!/bin/bash
# 使用. [shell file]的方式将子shell的环境导入到父shell中
. env.sh
# 如果env.sh执行有错,则输出error
. env.sh || echo "error"
# 使用括号包括会导致env.sh的环境无法传递到当前的shell进程中,环境会被封闭到括号区域内
(. env.sh) || (echo "error")
# 使用./[shell file]的方式无法将子shell的环境导入到当前shell中
./env.sh
# 子shell可以共享当前shell所获取的环境变量
./sub.sh
完整示例
## file: env.sh
#!/bin/bash
export env="here is env"
## file: boot.sh
#!/bin/bash
. env.sh || echo "error"
# 使用括号包括会导致env.sh的环境无法传递到当前的shell进程中
# (. env.sh) || (echo "error")
echo "boot"
echo $env
./sub.sh
## file: sub.sh
#!/bin/bash
echo "sub"
echo $env
文本处理
输入IPv4并检测有效性
ip=""
isMatch=""
while [[ "" == "$isMatch" ]]
do
read -p "please input ipv4 address: " ip
isMatch=`echo $ip | grep -Po "^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"`
done
sed -e "s/ip = \".*\"/ip = \"$ip\"/g" [your file]
按行读取
参考资料: