首页 > 系统相关 >shell脚本

shell脚本

时间:2023-11-16 18:56:41浏览次数:42  
标签:脚本 shell false echo && hadoop101 root localhost

1、变量

  1. 自定义变量;变量名=变量值{注意,:不能有空格,建议使用小写,系统变量都是大写}
    [root@hadoop101 ~]# name=zhangsan
  2. 引用变量值:$变量名 或 ${变量名}
    [root@hadoop101 ~]# name=zhangsan
    [root@hadoop101 ~]# echo $name
    zhangsan
    [root@hadoop101 ~]# echo ${name}
    zhangsan
    [root@hadoop101 ~]#
  3. 清空变量值unset变量名
    [root@hadoop101 ~]# unset name
  4. 间接引用
    1.  ${!变量名} ,把指定变量名对应的值解析为另⼀个变量名。
      [root@hadoop101 ~]# name=zhangsan
      [root@hadoop101 ~]# name2=$name
      [root@hadoop101 ~]# echo $name
      zhangsan
      [root@hadoop101 ~]# echo $name2
      zhangsan
      [root@hadoop101 ~]# zhangsan=28
      [root@hadoop101 ~]# echo ${!name2}
      28
      [root@hadoop101 ~]# echo ${name2}
      zhangsan
    2. 把命令的执⾏结果赋值给变量
      变量名=`命令` [注意;使⽤反xhxk]
      [root@hadoop101 ~]# var=`ls -l`
      [root@hadoop101 ~]# echo $var
      总⽤量 8 -rw-r--r--. 1 root root 79 11⽉ 14 07:32 aaa.sh -rwxr--r--. 1 root root 40 11⽉ 14 06:15 test.sh
    3. 表达式赋值:((变量名=表达式)),支持+-*/%不支持小数。
      [root@hadoop101 ~]# ((x=5+3))
      [root@hadoop101 ~]# echo $x
      8
      [root@hadoop101 ~]# y=5+3
      [root@hadoop101 ~]# echo $y
      5+3
  5. 变量的作用域全局变量,局部变量, 环境变量 [注:在⽂件定义的变量使用source运行,会在当前环境⽣成变量,当前环境就可以使用这个⽂件中定义的变量了]
    [root@hadoop101 ~]# source a1116.sh
    10
    [root@hadoop101 ~]# echo $z
    10
    [root@hadoop101 ~]# cat a1116.sh
    z=10
    echo $z
  6. 在脚本中使用特殊的变量
    1. $#表示传入脚本的参数个数。
    2. $0表示脚本名称。
    3. $*表示脚本的参数列表
    4. $n表示传入脚本的每个参数,n从1开始计数,如果参数大于10个
      $n 表示传⼊脚本的每个参数 ,n从1开始计数,如果参数⼤于10个,${10}
      [root@hadoop101 ~]# vim a1116.sh
      [root@hadoop101 ~]# cat a1116.sh
      #z=10
      #echo $z
      
      echo "$#这是传⼊脚本的参数个数"
      echo "$0脚本的名称"
      echo "$1传⼊脚本的第⼀个参数"
      echo "$2传⼊脚本的第⼆个参数"
      [root@hadoop101 ~]# bash a1116.sh aa bb 2这是传⼊脚本的参数个数
      a1116.sh脚本的名称
      aa传⼊脚本的第⼀个参数
      bb传⼊脚本的第⼆个参数
      [root@hadoop101 ~]# vim test01.sh
      [root@hadoop101 ~]# bash test01.sh zhangsan 18
      test01.sh,⽂件名
      zhangsan 18,参数列表
      姓名是:zhangsan
      年龄是:18
      [root@hadoop101 ~]# cat test01.sh
      echo "$0,⽂件名
      echo "$*,参数列表"
      echo "姓名是:$1"
      echo "年龄是:$2"
      
      [root@hadoop101 ~]#

2、键盘输入数据

 

read [ -p '提⽰字符串' -t 等待的秒数] 变量名 (如果输⼊错误,按退格键, 按ctrl+退格键删除)
[root@hadoop101 ~]# cat test02.sh
#!/bin/bash
read -p '请输⼊你的姓名' name
read -p '请输⼊你的年龄' age
echo -e "\n您的个⼈信息是:"
echo "您的姓名是:${name}"
echo "您的年龄是:${age}"

[root@hadoop101 ~]# bash test02.sh
请输⼊你的姓名张三
请输⼊你的年龄18

您的个⼈信息是:
您的姓名是:张三
您的年龄是:18
[root@hadoop101 ~]#

3、算术运算[注意:不运行浮点数运算]

  1. 运算符 +-*/%()   计算并赋值((变量名=表达式))
    [root@hadoop101 ~]# ((x=(5+3)*2))
    [root@hadoop101 ~]# echo $x
    16
    [root@hadoop101 ~]#
  2. 计算   $((表达式)) 或 $[表达式] 或 expr 操作数 运算符 操作数 [注意:必须有空格 乘号\*]
    [root@hadoop101 ~]# echo 5+3
    5+3
    [root@hadoop101 ~]# echo $((5+3))
    8
    [root@hadoop101 ~]# echo $[5+3]
    8
    [root@hadoop101 ~]# expr 5 + 3
    8
    [root@hadoop101 ~]# x=$[5+3]
    [root@hadoop101 ~]# echo $x
    8
    [root@hadoop101 ~]# y=`expr 5 + 3`
    [root@hadoop101 ~]# echo $y

4、多条语句写在一行时使用;或&&

[root@hadoop101 ~]# x=8;y=9
[root@hadoop101 ~]# echo $x
8
[root@hadoop101 ~]# echo $y
9
[root@hadoop101 ~]# echo $x;echo $y
8
9
[root@hadoop101 ~]# echo $x&&echo $y
8
9

5、条件测试命令 test [true 0 | false 1] 或 [条件] 或 [[条件]]

[root@hadoop101 ~]# test 1 -eq 1 && echo yes || echo no
yes
[root@hadoop101 ~]# test 1 -eq 2 && echo yes || echo no
no

6、数值判断: -eq 等于 -ne不等于 -gt⼤于 -ge ⼤于等于 -lt小于 -le小于等于 (equals, no, great,less, than)

[root@localhost ~]# test 1 -eq 1 && echo yes || echo no
yes
[root@localhost ~]# test 1 -eq 2 && echo yes || echo no
no
[root@localhost ~]# test 1 -ne 1 && echo yes || echo no
no
[root@localhost ~]# test 1 -gt 1 && echo yes || echo no
no
[root@localhost ~]# test 1 -ge 1 && echo yes || echo no
yes
[root@localhost ~]# test 1 -lt 1 && echo yes || echo no
no
[root@localhost ~]# test 1 -le 1 && echo yes || echo no
yes
[root@localhost ~]# [ 1 -eq 1 ] && echo yes || echo no
yes
[root@localhost ~]# [ 1 -lt 1 ] && echo yes || echo no
no
[root@localhost ~]# [[ 1 -eq 1 ]] && echo yes || echo no
yes
[root@localhost ~]# [[ 1 -lt 1 ]] && echo yes || echo no
no

7、逻辑运算符 !⾮ &&与-a || 或 -o (and or)

[root@localhost ~]# [ ! 2 -gt 0 -o 2 -gt 3 ] && echo true || echo false
false

8、字符串判断 -n 字符串长度不等于 0 为真(字符串不为空为真) -z字符串长度等于 0 为真(字符串为空时为真) 必须加双引号=相等 !=不相待 (判断符中间必须有空格隔开)

[root@localhost ~]# test -n "aaa" && echo true || echo false
true
[root@localhost ~]# 
[root@localhost ~]# test -n "" && echo true || echo false
false
[root@localhost ~]# test -z "" && echo true || echo false
true
[root@localhost ~]# test "aa" =  "aa" && echo true || echo false
true
[root@localhost ~]# [ "aa" = "ab" ] && echo true || echo false
false
[root@localhost ~]# [ "aa" == "ab" ] && echo true || echo false
false
[root@localhost ~]# [ "aa" != "ab" ] && echo true || echo false
true
[root@localhost ~]# [ "aa" != "aa" ] && echo true || echo false
false
[root@localhost ~]# test "aa" = "ab" && echo true || echo false
false
[root@localhost ~]# test "aa" == "ab" && echo true || echo false
false
[root@localhost ~]# [[ "aa" != "ab" ]] && echo true || echo false
true

9、⽂件判断 -e (exist)存在 -f (file)普通⽂件 -d (directory)⽬录 -s⾮空⽂件

[root@localhost ~]# [ -s bbb.sh ] && echo true || echo false
false
[root@localhost ~]# [ -e bbb.sh ] && echo true || echo false
false
[root@localhost ~]# [ -e cccc.sh ] && echo true || echo false
false
[root@localhost ~]# [ -d aaa.sh ] && echo true || echo false
false
[root@localhost ~]# [ -f aaa.sh ] && echo true || echo false
false
[root@localhost ~]# touch bbb.sh
[root@localhost ~]# [ -s aaa.sh ] && echo true || echo false
false
[root@localhost ~]# [ -e bbb.sh ] && echo true || echo false
true

10、条件语句

  1. if 条件 ; then 命令; [elif 条件; then 命令;] ... [ else 命令; ] fi
    [root@hadoop101 ~]# if true; then echo 123;fi
    123
    [root@hadoop101 ~]# if false; then echo 123;else echo 456; fi
    456
    [root@hadoop101 ~]# if true
    > then
    > echo 123
    > fi
    123
    [root@hadoop101 shell]# vim test1.sh
    [root@hadoop101 shell]# cat test1.sh
    #!/bin/bash
    if true
    then
    echo 123
    else
    echo 456
    fi
    [root@hadoop101 shell]# bash test1.sh
    123
  2. 例:键盘输⼊⼀个整数 判断是偶奇数
    [root@localhost shell]# vim test03.sh
    [root@localhost shell]# bash test03.sh
    请输入一个整数987
    987是奇数
    [root@localhost shell]# cat test03.sh
    #!/bin/bash
    read -p "请输入一个整数" x
    if [ $((x%2)) -eq 0 ]
    then
        echo "$x是偶数"
    else
        echo "$x是奇数"
    fi

     

 

标签:脚本,shell,false,echo,&&,hadoop101,root,localhost
From: https://www.cnblogs.com/hsk991213/p/17837022.html

相关文章

  • shell 常用技能
    1、重定向的使用 >/dev/null 2>&1拆分解释   1:>代表重定向到哪   2:/dev/null代表空设备文件   3:2>表示stderr标准错误,有0、1、2三种情况   4:&表示等同于的意思,2>&1,表示2的输出重定向等同于1   5:1表示stdout标准输出,系统默认值是1,所以">/dev/......
  • shell脚本定义变量和文件路径拼接
    在shell脚本定义变量为xx="xxx"例如把一个路径或文件名定义为一个变量inputPath="/mnt/RNASeq/Result"fileName="202308071824_210901003_2D230327074US2S2745DX"在路径"/mnt/RNASeq/Result"下面有多个文件夹,例如:L01、L02、···每个文件夹下存在多个fa文件,例如“2023080......
  • 基于pybind11实现C++程序中调用Python脚本增加C++程序扩展性
     文章目录前言一、pybind11与Python环境配置二、C++环境配置三、C++调用Python交互代码四、C++调用PythonDemo完整源码 前言Windows平台,在实际C++项目开发中,结合pybind11库,让python成为C++的脚本语言,可以大大提高C++程序的可扩展性,大大提高开发效率,特别......
  • CocosCreator脚本属性在属性面板的显示
    声明属性要声明属性,需要在cc.Class定义的properties字段中,填写属性名字和属性参数。cc.Class({extends:cc.Component,properties:{score:{default:0,type:cc.Integer,displayName:"分数",tooltip:"玩家的分数"}}}); 下拉列表要在属性检查器上显示为下拉......
  • Fiddler not intercepting PowerShell web requests
    FiddlernotinterceptingPowerShellwebrequestsFiddleractuallyworksviaaproxy,andautomaticallysetsproxysettingsinIEwhenrunning.Youcancheckthisbylaunchingfiddlerandthenchecking"LanSettings"underInternetOptions>......
  • JDK系列---【linux系统脚本快速安装JDK】
    1.install.shtar-zxvfjdk8.tar.gzmvjdk8u201jdk8#查看是否系统自带openjdkrpm-qa|grepjava#卸载自带openjdkrpm-etadata-java-2022a-1.el8.noarchjavapackages-filesystem-5.3.0-1.module+el8+2447+6f56d9a6.noarchjava-1.8.0-openjdk-headless-1.8.0.322.b06-11.......
  • Xshell7 显示需要更新安装版本,才能继续使用-解决
    昨天还在使用,今天打开的时候,就遇到Xshell7,提示"要继续使用此程序,您必须应用最新的更新或使用新版本"网上找了下解决方法,最终尝试一种方法进行了解决:方法一:更新电脑系统时间为2022-11-16日,再次打开xshell就可以使用了。但这种方法有个弊端,需要每次打开前都更新系统时间......
  • iframe本身就不是动态语言,样式和脚本都需要额外导入.iFrame的本质是内联框架的缩写,它
    以下哪个选项的描述是错误的Aiframe是用来在网页中插入第三方页面,早期的页面使用iframe主要是用于导航栏这种很多页面都相同的部分,这样在切换页面的时候避免重复下载Biframe的创建比一般的DOM元素慢了1-2个数量级Ciframe标签会阻塞页面的的加载Diframe本质是动态语言的Inc......
  • shell 循环while语句
    while条件 #while关键字,条件和if的条件一样,#while循环当条件为真的时候循环同时会一直循环,也就所说的死循环,为假时不循环do 循环体done#注意:while循环处理文件里面的行比较擅长,不管有没有空格都是一行。案例:#vimc.sh#!/usr/bin/bashi=1while[$i-lt50]do ......
  • noscript 元素用来定义在脚本未被执行时的替代内容(文本)
    NOSCRIPT标签是做什么用的?A制止脚本的运行B防止区域脚本被js修改(例如aDiv.innerHTML='something'将会不起作用)C用来定义在脚本未被执行时的替代内容DNOSCRIPT标签并不存在正确答案:Cnoscript元素用来定义在脚本未被执行时的替代内容(文本)。<body>......Y......