您的位置:首页 > 运维架构 > Shell

some tips about shell variables

2014-05-18 21:44 399 查看
1. 显示变量的长度,即${#var}

iubuntu@ubuntu:~$ aa="abc defg"

iubuntu@ubuntu:~$ echo ${#aa}

8

2. 查看当前正在使用shell的版本:

iubuntu@ubuntu:~$ echo $0

bash

iubuntu@ubuntu:~$ echo $SHELL

/bin/bash

3. 判断当前当前运行脚本是不是具有root权限:

#!/bin/bash

#filename: root.sh

if [ $UID -ne 0 ]; then

echo non root user

else

echo root user

fi

在运行sh root.sh过程中,出现了[: 7: -ne: unexpected operator,网上查了一下

因为ubuntu默认的sh是连接到dash的,又因为dash跟bash的不兼容所以出错了,解决办法有两种:

1)用bash root.sh来代替sh root.sh

2)修改sh默认连接到bash,sudo dpkg-reconfigure dash,选择no
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell command ubuntu bash