美文网首页
Linux shell

Linux shell

作者: 歌哥居士 | 来源:发表于2019-03-28 08:45 被阅读0次

文件头

#!/bin/sh

''、""、``

'' 表示字符串
"" 引用变量
`` 可以使用命令

$ x=5
$ echo '$x'
 $x
$ echo "$x"
 5
$ date
 2018年 2月18日 星期日 10时58分51秒 CST
$ date_val=`date`
$ echo "$date_val"
 2018年 2月18日 星期日 11时00分58秒 CST

变量

自定义变量

$ x=5
$ echo $x
5
#定义变量不要加空格,不然会被当成命令
$ x = 5
-bash: x: command not found

变量追加

$ x=123
$ x="$x"456
$ echo $x
123456

$ x=${x}789
$ echo $x
123456789

定义环境变量

$ export x=1
$ env | grep x=1
x=1

$ y=1
$ export y
$ env | grep y=1
y=1

环境变量与局部变量

pstree:查看进程树。环境变量是全局的,不论在子shell还是父shell,而用户自定义变量只在局部。

$ bash
$ pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─......
        ├─sshd───sshd───sshd───bash───bash───pstree
        ├─.......
$ export global_val=1
$ local_val=1
$ set
......
global_val=1
local_val=1

#进入子shell查看变量
$ bash
$ set
......
global_val=1

系统变量:PS1

$ echo $PS1
\h:\W \u\$

位置参数与预定义变量

位置参数变量 解释
$n n为数字。$0表示命令本身,$1...$9表示参数,10及以上要用大括号:${10}
$# 参数个数
$* 代表命令所有参数,将命令所有参数当成一个整体
$@ 代表命令所有参数,将命令每个参数区别对待
预定义变量 解释
$? 最后一次执行的命令的返回状态。正常返回0,非0不正常执行。
$$ 当前进程的PID
$! 后台运行的最后一个进程的PID
#!/bin/bash
num1=$1
num2=$2
sum=$(($num1+$num2))
echo $sum

执行
./param1.sh 20 30
50
#!/bin/bash
echo "------------------"
echo "\$#:    $#"

echo "------------------"
for i in "$*"
do
  echo "\$*:    $i"
done

echo "------------------"
for j in "$@"
do
  echo "\$@:    $j"
done



#./param2.sh 2 3 45 6
#输出结果:
------------------
$#:    4
------------------
$*:    2 3 45 6
------------------
$@:    2
$@:    3
$@:    45
$@:    6
$ echo "$?"
0
$ l
-bash: l: command not found
$ echo "$?"
127
$ echo $$
913
$ ps aux | grep 913
baozi              913   0.0  0.0  4300352   1640 s001  S     2:14下午   0:00.04 -bash
baozi              981   0.0  0.0  4267768    888 s001  S+    2:21下午   0:00.00 grep 913

运算符

一、使用declare

$ aa=11
$ bb=22
$ declare -i cc=$aa+$bb
$ echo $cc
33
$ declare -p cc
declare -i cc="33"

二、expr expr运算的时候注意+左右的空格

$ aa=11
$ bb=22
$ cc=$(expr $aa+$bb)
$ echo $cc
11+22
$ cc=$(expr $aa + $bb)
$ echo $cc
33

三、$(( 表达式 ))、$[ 表达式 ]

$ aa=11
$ bb=22
$ cc=$(( $aa+$bb ))
$ echo $cc
33
$ cc=$(( $aa + $bb ))
$ echo $cc
33
$ cc=$[ $aa + $bb ]
$ echo $cc
33
$ cc=$[ $aa+$bb ]
$ echo $cc
33

四、let

$ aa=11
$ bb=22
$ let cc=$aa+$bb
$ echo $cc
33
$ let cc=$aa + $bb
-bash: let: +: syntax error: operand expected (error token is "+")

变量测试符号

变量置换方式 变量y没有设置 变量y为空值 变量y设置值
x=${y-新值} x=新值 x为空 x=$y
x=${y:-新值} x=新值 x=新值 x=$y
x=${y+新值} x为空 x=新值 x=新值
x=${y:+新值} x为空 x为空 x=新值
x=${y=新值} x=新值
y=新值
x为空
y值不变
x=$y
y值不变
x=${y:=新值} x=新值
y=新值
x=新值
y=新值
x=$y
y值不变
x=${y?新值} 新值输出到标准错误输出(就是屏幕) x为空 x=$y
x=${y:?新值} 新值输出到标准错误输出(就是屏幕) 新值输出到标准错误输出(就是屏幕) x=$y

流程控制

判断文件类型

测试选项 作用
-d 判断文件是否存在且为目录
-f 判断文件是否存在且为普通文件
-e 文件是否存在
-b 判断文件是否存在且为块设备文件
-c 判断文件是否存在且为字符设备文件
-L 判断文件是否存在且为符号链接文件
-p 判断文件是否存在且为管道文件
-s 判断文件是否存在且非空
-S 判断文件是否存在且为套接字文件
示例:注意中括号左右要加空格
$ [-e some_passwd] && echo "yes" || echo "no"
-bash: [-e: command not found
no
$ [ -e some_passwd] && echo "yes" || echo "no"
-bash: [: missing `]'
no
$ [ -e some_passwd] && echo "yes" || echo "no"
yes

判断文件权限

测试选项 作用
-r 判断文件是否存在且拥有读权限
-w 判断文件是否存在且拥有写权限
-x 判断文件是否存在且拥有执行权限
-u 判断文件是否存在且拥有SUID权限
-g 判断文件是否存在且拥有SGID权限
-k 判断文件是否存在且拥有SBit权限

文件之间比较

测试选项 作用
文件1 -nt 文件2 判断文件1的修改时间是否比文件2的新。new time
文件1 -ot 文件2 判断文件1的修改时间是否比文件2的旧。old time
文件1 -ef 文件2 两个文件是否为同一个文件。equal file
$ ln some_passwd hard_some_passwd
$ ln -s some_passwd soft_some_passwd
$ ls -li
4298176865 -rw-r-xr-x  2 baozi  staff  130  2 19 16:40 hard_some_passwd
4298189532 lrwxr-xr-x  1 baozi  staff   11  2 19 18:52 soft_some_passwd -> some_passwd
4298176865 -rw-r-xr-x  2 baozi  staff  130  2 19 16:40 some_passwd
$ [ some_passwd  -ef hard_some_passwd ] && echo "yes" || echo "no"
yes
$ [ some_passwd  -ef soft_some_passwd ] && echo "yes" || echo "no"
yes

整数比较

测试选项 作用
整数1 -eq 整数2 等于。equal
整数1 -ne 整数2 不等于。not equal
整数1 -gt 整数2 大于。gleater than
整数1 -lt 整数2 小于。less then
整数1 -ge 整数2 大于等于。gleater or equal
整数1 -le 整数2 小于等于。less or equal
$ [ 20 -eq 20 ] && echo yes || echo no
yes

字符串比较

测试选项 作用
-z 字符串 判断为空。
-n 字符串 判断非空。
字符串1 == 字符串2 判断字符串相等。
字符串1 != 字符串2 判断字符串不等。
$ name="a"
$ [ -n $name ] && echo yes || echo no;
yes
$ name2="a"
$ [ $name == $name2 ] && echo yes || echo no;
yes

逻辑运算符

测试选项 作用
-a 逻辑与
-o 逻辑或
! 逻辑非

判断语句:if
if写法1

#!/bin/bash

cur_user=$(env | grep "^USER=" | cut -d "=" -f 2)

if [ "$cur_user" == "baozi" ]
  then
    printf '%s\n' $cur_user
fi

if写法2:其实就是让if和then在同一行,所以要加一个结尾符";"

#!/bin/bash

cur_user=$(env | grep "^USER=" | cut -d "=" -f 2)

if [ "$cur_user" == "baozi" ] ; then
  printf '%s\n' $cur_user
fi

if else

#!/bin/bash

read -p "input the dir:" dir
if [ -d "$dir" ] ; then
  printf '%s\n' "是个目录"
else
  printf '%s\n' "不是目录"
fi

多重if

#!/bin/bash
#提取后缀

read -p "input the filename:" filename
if [ -z "$filename" ] ; then
  echo "please input the filename"
  exit 1
fi

suffix=${filename##*\.}
if [ $suffix == "txt"  ] ; then
  printf '%s\n' "文件类型为txt"
elif [ $suffix == "sh"  ] ; then
  printf '%s\n' "文件类型为sh"
else
  echo "其他文件类型"
fi

判断语句:case

#!/bin/bash

read -p "输入yes或者no:" cho

case "$cho" in
  "yes")
    echo "yes"
    ;;

  "no")
    echo "no"
    ;;

   *)
    echo "other"
    ;;
esac

循环语句:for

#!/bin/bash

for i in 1 2 3 4 5
  do
    echo $i
  done
#!/bin/bash

for i in `seq 1 1000`
  do
    echo $i
  done
#!/bin/bash

s=0
for (( i=1;i<=100;i++ ))
  do
    let s=$s+$i
  done

echo $s

循环语句:while

#!/bin/bash

i=1
s=0
while [ $i -le 100 ]
  do
    let s=$s+$i
    let i=i+1
  done
echo $s

循环语句:util
与while相反,条件成立退出,不成立一直循环

相关文章

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • shell基础

    Linux学习 一、shell介绍 Linux shell基础 什么是shell shell是一个命令解释器,提供...

  • Linux下的shell

    Shell 和 Linux shell程序是包含若干行shell或linux命令的文件。linux命令语句只能一行...

  • linux shell脚本攻略笔记

    LINUX SHELL脚本攻略笔记[速查] linux shell脚本攻略笔记

  • Shell学习

    Linux Shell基础教程 (一) (二) Linux Shell简明教程(推荐) (一) (二) Linux...

  • Shell 脚本编程简介

    Shell菜鸟教程[https://www.runoob.com/linux/linux-shell.html] ...

  • shell 教程学习推荐地址

    [shell 教程学习推荐地址:] http://www.runoob.com/linux/linux-shell...

  • bash shell学习

    Shell 传递参数 参考链接:https://www.runoob.com/linux/linux-shell-...

  • shell脚本基础应用

    Shell 教程[https://www.runoob.com/linux/linux-shell.html]li...

  • Shell-基础教程-变量

    参考教程链接 http://www.runoob.com/linux/linux-shell.html Shell...

网友评论

      本文标题:Linux shell

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