shell脚本及举例:依次创建10个目录在相应目录下创建10个文件,在相应文件下添加内容

shell脚本

如果我们的命令或者应用程序不在命令行直接执行,而是通过一个程序文件来执行时,这个程序就被称之为shell脚本。Shell脚本里面通常内置了多条命令,有的还包含控制语句,比如if和else的条件控制语句,for和select的循环控制语句等。这些内置在一个shell脚本中的命令通常是一次性执行完成,不会不停的返回信息给用户,这种通过文件执行脚本的方式称之为非交互方式。Shell脚本类似于windows下的批处理,但是它比批处理要强大一些,现在windows下有一个叫做power shell的功能可以和linux下的shell功能媲美。我们可以在文本中输入一系列的命令、控制语句和变量,这一切有机的结合起来就形成了功能强大的shell脚本。Windows下的文本文件就是文本文件,不能被执行,但是linux下的文本文件只要将文档属性改成可以执行文件的,只要用chmod 777命令就可以了。

常见的脚本解释器

bash:是Linux标准默认的shell。bash由Brian Fox和Chet Ramey共同完成,是BourneAgain Shell的缩写,内部命令一共有40个。
sh: 由Steve Bourne开发,是Bourne Shell的缩写,sh 是Unix 标准默认的shell。
另外还有:ash、 csh、 ksh等。
:在shell中使用**#**进行注释,注意,sh里面没有多行注释,只能每一行加一个#号;

#!/bin/bash
# 上面中的 #! 是一种约定标记, 它可以告诉系统这个脚本需要什么样的解释器来执行;
echo "Hello, world!"
变量:

定义变量:

country="China"
Number=101

注意:
1、变量名和等号之间不能有空格;
2、首个字符必须为字母(a-z,A-Z)。
3、中间不能有空格,可以使用下划线(_)。
4、不能使用标点符号。
5、不能使用bash里的关键字(可用help命令查看保留关键字)。
使用变量时只需要在一个定义过的变量前面加上美元符号 $ 就可以了, 另外,对于变量的**{}** 是可以选择的, 它的目的为帮助解释器识别变量的边界.

Shell循环语句
  • for 循环
    一般格式为:
for 变量 in 列表
do
    command1
    ...
    commandN
done

注意:列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。
例如:
顺序输出当前列表的数字:

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done
  • while循环
    一般格式为:
while command
do
   Statement(s) to be executed if command is true
done

例如:

i=0
while [ $i -le 5 ]
do
    COUNTER='expr $i+1'
    echo $i
done
Shell函数

linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。
Shell 函数定义的语法格式如下:

function name() {
    statements
    [return value]
}

说明:

  • function是 Shell 中的关键字,专门用来定义函数;可以带function fun() 定义,也可以直接fun() 定义,不带任何参数
  • name是函数名;
  • statements是函数要执行的代码,也就是一组语句;
  • return value表示函数的返回值,其中 return 是 Shell 关键字,专门用在函数中返回一个值;这一部分可以写也可以不写。
shell函数参数

在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数…
带参数的函数示例:

funWithParam(){
    echo "The value of the first parameter is $1 !"
    echo "The value of the second parameter is $2 !"
    echo "The value of the tenth parameter is ${10} !"
    echo "The value of the eleventh parameter is ${11} !"
    echo "The amount of the parameters is $# !"  # 参数个数
    echo "The string of the parameters is $* !"  # 传递给函数的所有参数
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
输出结果:
第一个参数为 1 !
第二个参数为 2 !
第十个参数为 10 !
第十个参数为 34 !
第十一个参数为 73 !
参数总数有 11 个!
作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !

注意, 10 不 能 获 取 第 十 个 参 数 , 获 取 第 十 个 参 数 需 要 10不能获取第十个参数,获取第十个参数需要 10{10}。当n>=10时,需要使用${n}来获取参数。
在这里插入图片描述

shell脚本举例

要求:依次创建10个目录在相应目录下创建10个文件,在相应文件下添加内容

一般代码格式
#!/bin/bash
for((i=1;i<11;i++))
do
mkdir study.$i                       # 创建目录
done
echo "mkdir success!"
ls study.*                           # 显示目录
sleep 5
cd study.1                           # 进入study.1
for ((i=1; i<11; i++));
do
   touch test$i.txt
done                                 # 通过for在study.1目录下创建文件
for file in test1.txt test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test9.txt test10.txt
do
echo -e "11 22\n11 22\n11 22\n11 22\n11 22\n11 22\n11 22\n11 22\n11 22\n11 22" >> $file
done                                 # 通过echo命令向文件添加内容
    echo /root/ljh/study.2 /root/ljh/study.3 /root/ljh/study.4 /root/ljh/study.5 /root/ljh/study.6 /root/ljh/study.7 /root/ljh/study.8 /root/ljh/study.9 /root/ljh/study.10 | xargs -n 1 cp /root/ljh/study.1/test*.txt                                                                                         # cp命令复制study.1下的所有内容到其余9个目录
    
调用函数代码格式

通过函数创建10个目录,并进入相应的目录study.1下,在study.1目录下创建10个文件,在往这10个文件添加内容,最后通过while循环调用函数,并通过cp命令复制study.1下的所有内容到其余9个目录

#!/bin/bash
function fun1() {                      # 创建函数
    if [ $i -le 10 ];then
    for((i=1;i<=10;i++))
        do
    mkdir study.$i
    ls study.$i
        done
    cd study.1
    for((i=1;i<=10;i++))
        do
    touch test$i.txt
    echo -e "11 22\n11 22\n11 22\n11 22\n11 22\n11 22\n11 22\n11 22\n11 22\n11 22" >> test$i.txt
        done
    else
        echo "添加内容失败!"
    fi
    echo "The value of the first parameter is $i !"
}
i=1
while [ $i -le 10 ]
        do
    fun1                               # 调用函数
    ((i++))
       done
     cd ..
     echo /root/ljh/study.2 /root/ljh/study.3 /root/ljh/study.4 /root/ljh/study.5 /root/ljh/study.6 /root/ljh/study.7 /root/ljh/study.8 /root/ljh/study.9 /root/ljh/study.10 | xargs -n 1 cp /root/ljh/study.1/test*.txt

程序运行结果:

在这里插入图片描述

  • 3
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值