Shell命令及问题解决
Shell知识大多从菜鸟教程获取:http://www.runoob.com/linux/linux-shell.html
该网站还提供了在线编程功能很方便:http://www.runoob.com/try/runcode.php?filename=helloworld&type=bash
目录
1.字符串比较
2.执行sh ./xxx.sh出现:“Syntax error: “(” unexpected”的解决方法
3.循环删除数组中存放的目录
4.zsh->bash永久切换
正文
1. 字符串比较
bf=rwx
if [ "$bf" = "rwx" ];then
echo 'rwx'
else
echo 'not rwx'
fi
#执行结果:rwx
2. 执行sh ./xxx.sh出现:“Syntax error: “(” unexpected”的解决方法
因为Ubuntu/Debian为了加快开机速度,用dash代替了传统的bash,是dash在捣鬼,解决方法就是取消dash。
①$sudo dpkg-reconfigure dash
②在提示框中选择"no"
③重新执行sh xxxx.sh即可。
3.循环删除数组中存放的目录
file1=~/data/file
file2=~/var/user/file
file3=~/var/user/fileee
file4=~/var/user/setting/file
array=("$file1" "$file2" "file3" "file4")
for var in ${array[@]}
do
if [ ! -d $var ];then
echo [warning][not exist][$var]
else
rm -rf $var
echo [complete][deleted][$var]
fi
done
注:array初始化定义时,两个元素之间是空格,不是逗号。
4.zsh->bash永久切换
现象:linux环境编译工程时,报错“x86_64-linux-gcc only run on 64-bit linux”。而后在工程中grep “x86_64-linux-gcc only run on 64-bit linux” -r 后发现,是项目的脚本中echo报错信息。而同时,linux的vim也开始报错,错误内容是找不到 /usr/bin/zsh 脚本。原因:这两个问题都是默认shell设置有问题。
对策:修改默认shell为bash即可。网上有很多方法,可使用临时修改或永久修改。如果临时修改,则再开一个终端,默认shell仍是zsh。永久修改,下一次再开终端,shell会被改为bash。
永久修改shell方法:(zsh->bash)
①查看当前shell
$echo $SHELL
/bin/zsh
②修改passwd用户默认shell
$vim /etc/passwd
未修改前username 默认shell:
username:x:1000:1000:username,,,:/home/yangshuo:/usr/bin/zsh
↓修改后username默认shell:
username:x:1000:1000:username,,,:/home/yangshuo:/usr/bin/bash
③保存退出后,再次查看当前shell,若已经为/bin/bash,则修改成功。
$echo $SHELL
/bin/bash
④注销并重新登录username。