美文网首页
bash shell

bash shell

作者: LY_7z | 来源:发表于2021-09-09 17:30 被阅读0次
[root@centos7 tmp]# echo $SHELL
/bin/bash

打开终端时系统自动调用:/etc/profile 或 ~/.bashrc
/etc/profile
此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,系统的公共环境变量在这里设置,开始自启动的程序,一般也在这里设置.
~/.bashrc
用户自己的家目录中的.bashrc
登录时会自动调用,打开任意终端时也会自动调用
这个文件一般设置与个人用户有关的环境变量

#!/bin/bash
#!用来声明脚本由什么shell解释,否则使用默认shell
# 注释

脚本的3种执行方式:
1、bash test.sh
2、chmod +x test.sh 加权限再执行 ./test.sh
3、source test.sh 或 . test.sh
我们指定解释器的时候,会创建一个子shell解析脚本
注意:Windows下写的脚本,在Linux中执行,需转换成Unix文件
方法1、sudo apt-get install dos2unix dos2unix test.sh
方法2、vi 打开脚本, :set ff=unix

定义变量 num=10 等号两边不能有空格
引用变量 $num
清除变量 unset num
查看环境变量 env
从键盘获取值 read -p

#!/bin/bash
num=10
echo $num
echo "enter your age:"
read  age
echo $age
echo  -p  "enter your  address:"  address      #在一行上显示和添加提示 需要加上-p
echo $address
read  name  age  city    #读取多个值
echo  name=$name    age=$age  city=$city
readonly    num=20     #只读变量

用source 或 . 执行脚本 和 bash 执行脚本的区别:

[root@centos7 tmp]# cat 1.sh
#!/bin/bash
export AGE=10
echo $AGE

变量名只能包含英文字母下划线,不能以数字开头
等号两边不能直接接空格符,若变量中本身就包含了空格,则整个字符串都要用双引号、或单引号括起来
双引号 单引号的区别
双引号:可以解析变量的值
单引号:不能解析变量的值

[root@centos7 tmp]# echo $num
10
[root@centos7 tmp]# echo "$num"
10
[root@centos7 tmp]# echo '$num'
$num
在PATH变量中 追加一个路径写法如下:
export  PATH=$PATH:/需要添加的路径

shell内置变量

$#  传递给shell脚本参数的数量
$*  传给shell脚本参数的内容
$1  $2  $3  ...  运行脚本时,传递给其的参数,用空格隔开
$?   用于检查上一条命令是否被成功执行  0表示成功,非0表示出错
$0  当前执行的脚本名称
$$  当前进程的进程号
[root@centos7 tmp]# cat 3.sh
#!/bin/bash
echo "参数的个数=$#"
echo "参数的内容=$*"
echo "第一个参数:$1"
echo "第二个参数:$2"
echo "第三个参数:$3"
readonly num=10
num=20
echo "num=20是否执行成功:$?"
echo "当前脚本执行的名称是:$0"
echo "当前脚本执行的进程号是:$$"

[root@centos7 tmp]# bash 3.sh 4 5 6
参数的个数=3
参数的内容=4 5 6
第一个参数:4
第二个参数:5
第三个参数:6
10
是否执行成功:0
当前脚本执行的名称是:3.sh
当前脚本执行的进程号是:25466

双引号内包含的变量会被解释
单引号内包含的变量会被当做普通字符串
反引号内包含的系统命令会被优先执行
转义字符
小括号()内的命令在子shell中执行,不影响当前shell中的变量
花括号{}内的命令在当前shell中执行,会影响当前变量

[root@centos7 tmp]# a=10
[root@centos7 tmp]# (a=20;echo $a)
20
[root@centos7 tmp]# echo $a
10
[root@centos7 tmp]# echo "a的值是$a"
a的值是10
[root@centos7 tmp]# echo 'a的值是$a'
a的值是$a
[root@centos7 tmp]# echo "今天是`date`"
今天是2021年 09月 09日 星期四 13:44:24 CST
[root@centos7 tmp]# echo -e "hi\new student\this is your \table"
hi
ew student      his is your     able
判断变量是否存在
[root@centos7 tmp]# cat 4.sh
#!/bin/bash
# ${num1:-num2}  如果num1存在,整个表达式的值为num1的值,否则为num2,注意中间是 :- 格式 
echo ${num:-40}
num=50
echo ${num:-40}
[root@centos7 tmp]# bash 4.sh
40
50
还有这种格式 ${num1:=num2}  如果num1存在,表达式的值为num1, 否则为num2,同时将num1的值赋给num2
[root@centos7 tmp]# echo $num
10
[root@centos7 tmp]# echo ${num:=100}
10
下面的变量c是不存在的,所以返回了100
[root@centos7 tmp]# echo ${c:=100}
100
[root@centos7 tmp]# echo $c
100

字符串操作

#计算字符串长度  ${#}
[root@centos7 tmp]# str="my name is bash shell"
[root@centos7 tmp]# echo "str字符串的长度是${#str}"     
#从指定位置开始提取字符串  
[root@centos7 tmp]# echo "从第3个位置开始提取字符串--->${str:3}"
从第3个位置开始提取字符串--->name is bash shell
#从位置3开始,截取后面7位
[root@centos7 tmp]# echo "从第3个位置开始提取字符串--->${str:3:7}"
从第3个位置开始提取字符串--->name is
[root@centos7 tmp]# echo "用A替换字符串中出现的第一个a--->${str/a/A}"
用A替换字符串中出现的第一个a--->my nAme is bash shell
[root@centos7 tmp]# echo "用A替换字符串中出现的所有a--->${str//a/A}"
用A替换字符串中出现的所有a--->my nAme is bAsh shell
条件测试

test 条件
[ 条件 ] 使用方括号时,要注意在条件两边加上空格。
文件测试
-e 是否存在 -d 是否是目录 -f 是否是文件 -r 可读 -w 可写 -x 可执行 -s 文件是否非空

[root@centos7 tmp]# test -e 1.sh
[root@centos7 tmp]# echo $?
0
[root@centos7 tmp]# [ -e 5.sh ]
[root@centos7 tmp]# echo $?
1

数值测试
-eq 相等 -ne不相等 -gt大于 -lt小于 -ge大于等于 -le小于等于
命令执行控制 && ||
多重判断 -a -o !

控制语句 if case for while break until

if  [ 条件1 ];  then
     语句1
else
     语句2
fi
格式二
if  [ 条件1 ];  then
     语句1
elif [ 条件2 ];  then
     语句2
else
     语句3
fi

[root@centos7 tmp]# cat 5.sh
#!/bin/bash
read -p "enter y/n continue:"  answer
if [ $answer = "y" ]; then
        echo "continue exec."
else
        echo "stop exec."
fi


[root@centos7 tmp]# cat 6.sh
#!/bin/bash
read -p "输入文件夹的名称:" dir_name
if [ -e $dir_name ]; then
        echo "$dir_name文件夹存在"
        cd $dir_name
        echo "创建文件名为file01.txt"
        touch file01.txt
else
        echo "该文件夹不存在,开始创建"
        mkdir $dir_name
        echo "进入$dir_name文件夹"
        cd $dir_name
        echo "创建文件file02.txt"
        touch file02.txt
fi
tree

case用法

case  $变量名 in
          第一个变量的值
           代码1
            ;;
           第二个变量的值
           代码2
            ;;
          *)
           其他代码
           exit   1
esac


[root@centos7 tmp]# cat 7.sh
#!/bin/bash
read -p "input yes/no:" chioce
case $chioce in
        yes | y* | Y*)
                echo "start"
                ;;
        no | n* | N*)
                echo "stop"
                ;;
        *)
                echo "error"
                ;;
esac

for用法

[root@centos7 tmp]# cat 8.sh
#!/bin/bash
for file_name in `ls`
do
        if [ -d $file_name ]; then
                echo "$file_name is dir"
        elif [ -f $file_name ]; then
                echo "$file_name is file"
        fi
done

while用法

[root@centos7 tmp]# cat 9.sh
#!/bin/bash
declare -i i
declare -i s
while [ "$i" != "101" ]
do
        s+=i;
        i=i+1
done
echo "the count is $s"

break continue 用法

函数

函数名(){
命令
}

function 函数名() {
命令
}
所有函数在使用前必须定义,必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用
调用函数 直接写函数名就可以,不需要加()

相关文章

  • chapter 3.基本的bash shell命令

    基本的bash shell命令 启动shell 大多数Linux默认的shell都为GNU bash shell/...

  • Homebrew相关问题

    查看当前shell echo $SHELL zsh切换bash chsh -s /bin/bash bash切换z...

  • shell & bash基础命令及巧用

    shell与bash脚本的区别shell是Linux基础命令解释器bash(Bourne Again shell)...

  • Shell补课

    更改启动shell chsh -s /bin/bash shell目录文件 /etc/shells 区别.bash...

  • shell 编程学习

    当前shell执行命令。./或者source 新建shell:/bin/bash ./file.sh bash $...

  • Bash

    主要概念 Linux默认的shell是bash Shell bash Shell 可以交互使用,或者作为一种强大的...

  • Day-2初识linux

    1.bash shell 是什么? bash shell 是一个命令解释器,用户输入命令之后,通过bash she...

  • bash环境配置文件

    longin shell 输入用户名密码取得的bash nologin shell bash的子进程 ./etc/...

  • 第1节 shell脚本创建执行和输出

    #shell脚本创建执行和输出 1.shell脚本开头 #!/bin/bash /bin/bash 是Ba...

  • shell变量

    bash 大多数Linux系统默认使用的shell,bash shell 是shell 的一个免费版本,它是最早的...

网友评论

      本文标题:bash shell

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