美文网首页
shell命令2

shell命令2

作者: crossroads | 来源:发表于2020-03-03 15:58 被阅读0次

1. 运算符

echo `expr 2 + 2` //4 ,记得空格哦
a=$b // 将把变量 b 的值赋给 a
a='a'
b='a'
if [ $a == $b ] // 判断,[ $a = $b ]也可以
then
  echo "相等"
fi

2. 文件测试运算符

file="/Users/mine/Downloads" 
if [ -d $file ]
then
   echo "文件是个目录"
else
   echo "文件不是个目录"
fi

3. echo命令

read name //输入行的字段指定给shell变量
echo "It is a test" > myfile //显示结果定向至文件
echo `date` //显示命令执行结果 

4. printf命令
printf不像echo会自动换行,需要手动加换行符
格式:printf format-string [arguments...]

printf "hello\n"
//%s %d 都是格式替代符,%-10s 指一个宽度为10个字符(-表示左对齐,没有则表示右对齐
printf "%9d %5d" 1000 1000//     1000  1000% //
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg //姓名         性别       体重kg

5. test命令
用于检查某个条件是否成立

//文件是否存在
if test -e ./bash
then
    echo '文件已存在!'
else
    echo '文件不存在!'
fi
参数 说明
-e 文件存在
-r 存在且可读
-w 存在且可写
-x 存在且可执行
-s 如果文件存在且至少有一个字符
-d 如果文件存在且为目录
-f 如果文件存在且为普通文件
-c 如果文件存在且为字符型特殊文件
-b 如果文件存在且为块特殊文件

6. if语句

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

举个例子

a=10
b=10
if [ $a -eq  $b ]
then
   echo "a 等于 b"
elif [ $a -gt $b ]
then
   echo "a 大于 b"
else
   echo "没有符合的条件"
fi
//写成一行
 if [ $a -eq  $b ]; then echo "a等于b";fi
a等于b

7.for语句

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done
//按顺序输出字符串中的字符
for str in 'hello'
do
    echo $str
done

for item in 1 2 3 
do
    echo " $item"
done

8. while语句

while condition
do
    command
done
int=1
while (( $int <= 5 ))
do
    echo $int
    let int++  
done

9. until语句
直至条件为true时停止

until condition
do
    command
done

10. break和continue
这个不做叙述,和其他语言的一样
11. case语句

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac
echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
    1)  echo '你选择了 1'
    ;;
    2)  echo '你选择了 2'
    ;;
    3)  echo '你选择了 3'
    ;;
    4)  echo '你选择了 4'
    ;;
    *)  echo '你没有输入 1 到 4 之间的数字'
    ;;
esac

后记

printf用法详解参考网站: https://blog.csdn.net/sugarbliss/article/details/80230710
学习网站:https://www.runoob.com/linux/linux-shell-variable.html
运行sh命令:https://www.runoob.com/try/runcode.php?filename=helloworld&type=bash

相关文章

  • shell基础

    shell脚本 1.shell脚本执行方式 1.1 添加执行权 1.2 指定shell命令 2.shell变量 2...

  • vim学习 09——shell命令

    vim学习 09——shell命令 执行 shell 命令 :!shell命令 : 可以执行 shell 命令。 ...

  • MongoDB入门

    1. 启动 2. 启动mongo shell 3. shell 命令

  • adb命令

    常用非Shell命令:1、adb shell dumpsys activity top2、adb shell du...

  • Fabric01-1

    一:shell脚本基础 1.shell命令 : 在linux终端能够被解析出来的命令2.shell脚本 : 多个s...

  • 嵌入式学习笔记19.11.20

    Linux 的shell基本命令: shell的版本:1.Bourne Shell(sh) 2.C Shell(c...

  • HADOOP-HDFS(2)

    hdfs使用两种方式:(1)shell命令(2)API编程 shell命令 两种风格:(a)hadoop fs (...

  • Shell命令汇总

    1、一般常用命令 (1)Shell 常用命令总结 (2)Shell的18条常用命令整理 2、文件操作常用命令 (1...

  • ADB常用命令集合

    基础命令 USB设备命令 文件传输命令 SHELL命令 adb shell pm命令 adb shell am命令...

  • (linux/mac)mysql调用系统命令

    system + shell命令! + shell命令

网友评论

      本文标题:shell命令2

      本文链接:https://www.haomeiwen.com/subject/ysatchtx.html