Linux:shell命令
Linux:shell命令
Shell echo命令
显示普通字符串
echo "i am wsf"
AI写代码
shell
1
显示转义字符
echo "i\" am usf"
AI写代码
shell
1
显示变量
read name#相当于python中的input
echo "${name} is a name"
read -p "请输入您的名字:" names#提示
echo "${names} is a names"
显示换行
echo -e "i am fine \n"
echo " irt is finr"
显示不换行
echo -e "ok \c"
echo "i am fine"
显示结果定向至文件
echo "i am ok" > myfile
原样输出字符串,不进行转义或取变量(用单引号)
echo '$name\"'
显示命令执行结果
echo `date`
Shell read命令
read命令:读取从键盘输入的数据
read a b c #使用空格分开
echo $a $b $c
read -p "请输入您的年龄" age
echo "您的年龄是:${age}"
read -p "请输入您的年龄" -t 3 age
echo
echo "您的年龄是:${age}"
read -p "请输入您的年龄" -s -t 5 age
echo
echo "您的年龄是:${age}"
Shell printf命令
printf 3命令
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg#(- 表示左对齐,没有则表示右对齐)
printf "%-10s %-8s %-4.2f\n" 张三 男 55.4238
printf "%-10s %-8s %-4.2s\n" 李四 男 88.12354
printf "%-10s %-8s %-4.2f\n" 王五 女 55
printf "%-10s %-8s %-4.2f\n" absccfjkajkf 女 55
#-10s 指一个宽度为 10 个字符,,如果不足则自动以空格填充,超过也会将内容全部显示出来。
转义字符
Shell test命令
test 命令用于检查某个条件是否成立
a=5
b=3
if test $a = $b;
then
echo "两数相等"
else
echo "两数不相等"
fi
if test -z $a;
then
echo "字符串长度为0"
else
echo "字符串长度不为0"
fi
文件测试
if test -e ./demo.sh;
then
echo "存在"
else
echo "不存在"
fi
if test -w ./demo.sh;
then
echo "存在且可读"
else
echo "不存在或不可读"
fi