批处理命令实例(批处理命令是什么语言)

批处理命令实例(批处理命令是什么语言)h4 1 任务描述 h4 编写简单的 shell 脚本 使用输入输入重定向及管道符将脚本的信息重定向到文件 2 任务实施 1 创建 shell 脚本 firstscript sh 使用 vim 文本编辑器在用户家目录下创建一个新的文本文件 将其命名为 firstscript sh

大家好,我是讯享网,很高兴认识大家。



 <h4>1.任务描述</h4> 

讯享网

编写简单的shell脚本,使用输入输入重定向及管道符将脚本的信息重定向到文件。

2.任务实施

(1)创建shell脚本firstscript.sh,使用vim文本编辑器在用户家目录下创建一个新的文本文件,将其命名为firstscript.sh,插入以下文本并保存文件,将输入重定向到文件中。

讯享网[root@localhost ~]# vim input.txt [root@localhost ~]# cat input.txt 2022 2021 1995 2004 0024 2654 4505 [root@localhost ~]# vim firstscript.sh [root@localhost ~]# cat firstscript.sh ​ #!/bin/bash # 从文件中读取输入 sort < input.txt ​ ​

(2)使用bash命令执行脚本。

[root@localhost ~]# chmod +x firstscript.sh [root@localhost ~]# bash +x firstscript.sh 0024 1995 2004 2015 2021 2022 2654 4505 [root@localhost ~]# cat input.txt 2022 2021 1995 2004 0024 2654 4505 2015

(3)将输出写入文件中

讯享网[root@localhost ~]# ls -l > output.txt

(4)追加输出到文件中。

[root@localhost ~]# ls -l >> output.txt

(5)将标准错误输出重定向到文件中。

讯享网[root@localhost ~]# ls -l /non-existent-dir 2>error.log

(6)使用输入重定向忽略read命令的输入。

[root@redhat01 ~]# vim file.sh [root@redhat01 ~]# cat file.sh #!/bin/bash # 忽略read命令的输入 read -p "Enter your name: " name < /dev/null echo "Your name is : $name"

(7)从标准输入中读取多行文本。

讯享网[root@localhost ~]# vim file.sh [root@localhost ~]# cat file.sh #!/bin/bash echo "Enter some text (Ctrl+D to finish):" cat << EOF This is line 1 This is line 2 This is line 3 EOF

(8)将多行文本输入到文件。

​ [root@localhost ~]# vim file.sh [root@localhost ~]# cat file.sh #!/bin/bash cat << EOF > output.txt This is new line 1 This is new line 2 This is new line 3 EOF

(9)将多行文本追加到文件。

讯享网[root@localhost ~]# vim file.sh [root@localhost ~]# cat file.sh #!/bin/bash cat << EOF >> output.txt This is line 4 This is line 5 This is line 6 EOF

1.任务描述

编写shell脚本,通过数据输入输出与用户交互,使用户输入数据或输出信息;通过数据输入输出读取文件中的数据或写入数据到文件中;通过数据输入输出可以与其他程序或系统交互,以获取或输出数据。使用read命令读取用户输入的数据,使用echo或printf命令输出信息

2.任务实施

(1)使用read命令读取用户输入的数据。

[root@localhost ~]# vim printf01.sh [root@localhost ~]# cat printf01.sh #!/bin/bash # 读入用户输入的数字 read -p "Enter a number: " num # 输出用户输入的数字 echo "You entered: $num" # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x printf01.sh [root@localhost ~]# bash printf01.sh Enter a number: 100 You entered: 100

(2)使用read命令读取多个数据。

讯享网[root@localhost ~]# vim printf02.sh [root@localhost ~]# cat printf02.sh #!/bin/bash # 读取多个数据 read -p "Enter your name, age and gender: " name age gender # 输出数据 echo "Name: $name" echo "Age: $age" echo "Gender: $gender" # 执行脚本并查看输出结果 [root@redhat01 ~]# bash printf02.sh Enter your name, age and gender: Tom 18 male Name: Tom Age: 18 Gender: male ​

(3)使用read命令读取文件中的每一行内容。

[root@localhost ~]# vim file.txt [root@localhost ~]# cat file.txt www.opencloud.fun www.redhat.com Linux Shell and Ansible [root@localhost ~]# vim printf03.sh [root@localhost ~]# cat printf03.sh #!/bin/bash # 读取文件中的每一行内容 while read line; do        # 输出读取的每一行内容        echo $line done < file.txt # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x printf03.sh [root@localhost ~]# bash printf03.sh www.opencloud.fun www.redhat.com Linux Shell and Ansible

(4)使用printf命令格式化输出数字。

讯享网[root@localhost ~]# vim printf04.sh [root@localhost ~]# cat printf04.sh #!/bin/bash printf "%.2f " 3. printf "%.4f " 3. printf "%d "  printf "%x " 255 # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x printf04.sh [root@localhost ~]# bash printf04.sh 3.14 3.1416  ff

(5)使用printf命令格式化输出字符串。

[root@localhost ~]# vim printf05.sh [root@localhost ~]# cat printf05.sh#!/bin/bash printf "%-10s %-8s %-4s " Name Gender Age printf "%-10s %-8s %-4d " John Male 30 printf "%-10s %-8s %-4d " Mary Female 25 # 执行脚本并查看输出结果 [root@localhost ~]# bash printf05.sh Name       Gender   Age John       Male     30   Mary       Female   25  

(6)使用printf命令输出多个字符和字符串。


讯享网

讯享网[root@localhost ~]# vim printf06.sh [root@localhost ~]# cat printf06.sh #!/bin/bash printf "%s %s %s " A B C printf "%s %s %s " A B C D E F G # 定义字符变量 char='a' # 使用%c格式化输出字符 printf "The character is %c. " $char # 定义字符串 string="Hello,World!" # 使用%s格式化输出字符串 printf "The string is %s. " "$string" # 使用%-10s格式化输出左对齐的字符串 printf "%-10s " "$string" # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x printf06.sh [root@localhost ~]# bash printf06.sh A B C A B C D E F G   The character is a. The string is Hello,World!. Hello,World!

(7)使用printf命令输出变量值。

[root@localhost ~]# vim printf07.sh [root@localhost ~]# cat printf07.sh#!/bin/bash name="John" age=30 printf "My name is %s, and I am %d years old. " "$name" "$age" # 上面的脚本中定义两个变量:name 和 age # 使用printf命令输出字符串,并使用%s和%d占位符引用变量 # printf命令可以使用多个参数,使用变量时需要使用$调用变量 # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x printf07.sh [root@localhost ~]# bash printf07.sh My name is John, and I am 30 years old.

1.任务描述

在shell中,变量主要用于保存和引用各种类型的数据,可以用于存储各种信息,如字符串、数字等。变量在shell中非常常用, 可以用于存储用户输入的数据、存储程序运行过程中产生的数据、存储程序执行结果,还可以用于条件判断和循环控制、存储文件名和路径、文件处理、存储环境变量、程序的配置和运行等。

2.任务实施

(1)编写shell脚本,使用变量获取主机的内存信息、网络互联网协议(Internet Protocol ,IP)地址、CPU负载等信息。

讯享网[root@localhost ~]# vim systeminfo-output.sh   [root@localhost ~]# cat systeminfo-output.sh #!/bin/bash # 获取内存信息 memory=$(free -m | awk 'NR==2{printf "Total: %sMB, Used: %sMB, Free: %sMB", $2, $3, $4}') # 获取网络IP地址 ip=$(ip addr | grep 'inet' | grep -v 'inet6' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d '/' -f 1) # 获取CPU负载 cpu=$(top -bn1 | grep 'Cpu(s)' | awk '{print $2}' | cut -d '%' -f 1) # 输出信息 echo "Memory: $memory" echo "IP: $ip" echo "CPU: $cpu%" # 注意,在使用变量获取信息时,需要使用$(...)语法来执行命令并将结果赋给变量 # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x systeminfo-output.sh [root@localhost ~]# bash systeminfo-output.sh Memory: Total: 1923MB, Used: 1299MB, Free: 95MB IP: 192.168.6.140 CPU: 2.6%

(2)编写shell脚本,输出$0、$1、$2、$3、$@、$#、$!、$?、$*、$$等位置参数的变量信息。

[root@localhost ~]# vim location-output.sh [root@localhost ~]# cat location-output.sh #!/bin/bash # 提示用户输入他们的姓名 echo "Please enter your name: " read name # 输出各变量的值 echo "$0 is: $0" echo "$1 is: $1" echo "$2 is: $2" echo "$3 is: $3" echo "$@ is: $@" echo "$# is: $#" echo "$! is: $!" echo "$? is: $?" echo "$* is: $*" echo "$$ is: $$" echo "Name is: $name" # 执行脚本并查看输出结果 [root@redhat01 ~]# chmod +x location-output.sh [root@redhat01 ~]# bash location-output.sh one two three Please enter your name: one two three $0 is: location-output.sh $1 is: one $2 is: two $3 is: three $@ is: one two three $# is: 3 $! is: $? is: 0 $* is: one two three $$ is: 1906 Name is: one two three ​

(3)输出当前目的的日历信息,并使用printf命令格式化输出

讯享网[root@localhost ~]# vim date-output.sh [root@localhost ~]# cat date-output.sh #!/bin/bash # 获取当前月份和年份 month=$(date +%m) year=$(date +%Y) # 使用cal命令获取当前月的日历 calendar=$(cal $month $year) # 使用printf命令格式化输出 printf "Calendar for %s %s: " $month $year printf "%s " "$calendar" # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x date-output.sh [root@localhost ~]# bash date-output.sh Calendar for 10 2024: ​     十月 2024     一 二 三 四 五 六 日    1  2  3  4  5  6 7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31         

(4)编写shell脚本,使用变量并结合printf命令格式化输出当前系统的磁盘分区、swap分区、逻辑卷等信息。

[root@localhost ~]# vim disk-output.sh [root@localhost ~]# cat disk-output.sh #!/bin/bash # 使用df命令获取磁盘分区信息 disk_partitions=$(df -h) # 使用swapon命令获取swap分区信息 swap_partitions=$(swapon -s) # 使用lvdisplay命令获取逻辑卷信息 logical_volumes=$(lvdisplay) • # 使用printf命令格式化输出 printf "Disk Partitions: " printf "%s " "$disk_partitions" printf " Swap Partitions: " printf "%s " "$swap_partitions" printf " Logical Volumes: " printf "%s " "$logical_volumes" # 执行脚本并查看结果 [root@localhost ~]# chmod +x disk-output.sh [root@localhost ~]# bash disk-output.sh 文件系统               容量 已用 可用 已用% 挂载点 devtmpfs               4.0M     0  4.0M    0% /dev tmpfs                 962M     0 962M    0% /dev/shm tmpfs                 385M  7.8M 377M    3% /run /dev/mapper/rhel-root   17G  4.2G   13G   26% / /dev/nvme0n1p2         960M 260M 701M   27% /boot /dev/nvme0n1p1         599M  7.0M 592M    2% /boot/efi tmpfs                 193M   96K 193M    1% /run/user/0 /dev/sr0               9.9G  9.9G     0  100% /run/media/root/RHEL-9-3-0-BaseOS-x86_64 ​ Swap Partitions: ​ Filename                             Type             Size           Used Priority /dev/dm-1                               partition              0    -2 ​ Logical Volumes: ​  --- Logical volume --- LV Path               /dev/rhel/swap LV Name               swap VG Name               rhel LV UUID               XWP4aL-fBi0-awzt-dKcE-3ZC4-rg7Z-69RyS2 LV Write Access       read/write LV Creation host, time localhost.localdomain, 2024-04-29 20:41:33 +0800 LV Status             available  # open                 2 LV Size                2.00 GiB Current LE             512 Segments               1 Allocation             inherit Read ahead sectors     auto  - currently set to     256 Block device           253:1    --- Logical volume --- LV Path               /dev/rhel/root LV Name               root VG Name               rhel LV UUID               x2qTw4-DaXP-cWTP-tP00-ae83-0bgt-CbzqTv LV Write Access       read/write LV Creation host, time localhost.localdomain, 2024-04-29 20:41:33 +0800 LV Status             available  # open                 1 LV Size                16.41 GiB Current LE             4201 Segments               1 Allocation             inherit Read ahead sectors     auto  - currently set to     256 Block device           253:0    --- Logical volume --- LV Path               /dev/vg-group01/databackup LV Name               databackup VG Name               vg-group01 LV UUID               DB2f1A-ml0c-mqDU-nNG3-Bzea-OfMa-6SDfQo LV Write Access       read/write LV Creation host, time localhost.localdomain, 2024-04-30 15:01:04 +0800 LV Status             NOT available LV Size                8.00 GiB Current LE             2048 Segments               1 Allocation             inherit Read ahead sectors     auto

1.任务描述

在shell中,算术运算主要用于执行各种数学计算。常见的算术运算包括加法、减法、乘法、除法、求余数、幂运算等。

(1)编写shell脚本,计算三角形的面积、圆的面积和周长,输出结果。

讯享网[root@localhost ~]# vim calculate01.sh [root@localhost ~]# cat calculate01.sh #!/bin/bash # 定义三角形的底和高 triangle_base=6 triangle_height=8 # 计算三角形的面积 triangle_area=$(echo "scale=2; $triangle_base * $triangle_height / 2" | bc) # 定义圆的半径 circle_radius=10 # 计算圆的面积 circle_area=$(echo "scale=2; 3.14 * $circle_radius * $circle_radius" | bc) # 计算圆的周长 circle_circumference=$(echo "scale=2; 2 * 3.14 * $circle_radius" | bc) # 输出三角形的面积、圆的面积和周长 echo "三角形面积:$triangle_area" echo "圆的面积:$circle_area" echo "圆的周长:$circle_circumference" # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x calculate01.sh [root@localhost ~]# bash calculate01.sh 三角形面积:24.00 圆的面积:314.00 圆的周长:62.80

(2) 编写shell脚本,使用bc命令进行算术运算。

[root@redhat01 ~]# vim calculate03.sh [root@redhat01 ~]# cat calculate03.sh #!/bin/bash # 定义变量 a=10 b=20 c=30 # 使用let命令进行算术运算 let result1=a+b let result2=a*b let result3=c%a let result4=ab let result5=a+b*c let result6=a*b+c let result7=c%a+b # 输出结果 echo "a + b = $result1" echo "a * b = $result2" echo "c % a = $result3" echo "a ^ b = $result4" echo "a + b * c = $result5" echo "a * b + c = $result6" echo "c % a + b = $result7" # 执行脚本并查看结果 [root@localhost ~]# chmod +x calculate02.sh [root@localhost ~]# bash calculate02.sh a + b = 30 a * b = 200 a / b = .50 c % a = 0 a ^ b = 0000000000 a + b * c = 610 a * b + c = 230 c % a + b = 20

(5)编写shell脚本,使用$((...))表达式进行算术运算。

讯享网[root@localhost ~]# vim calculate05.sh [root@localhost ~]# cat calculate05.sh #!/bin/bash # 定义变量 a=10 b=20 c=30 # 计算结果 result1=$((a + b)) result2=$((a * b)) result3=$((a / b)) result4=$((a % b)) result5=$((a b)) result6=$((a + b * c)) result7=$((a * b + c)) result8=$((c % a + b)) # 输出结果 echo "a + b = $result1" echo "a * b = $result2" echo "a / b = $result3" echo "a % b = $result4" echo "a ^ b = $result5" echo "a + b * c = $result6" echo "a * b + c = $result7" echo "c % a + b = $result8" # 执行脚本并查看输出结果 [root@localhost ~]# chmod +x calculate05.sh [root@localhost ~]# bash calculate05.sh a + b = 30 a * b = 200 a / b = 0 a % b = 10 a ^ b =  a + b * c = 610 a * b + c = 230 c % a + b = 20 ​

任务五 设置环境变量

在Linux操作系统中,设置java环境变量涉及JAVA_HOME和PATH两个主要的环境变量,将环境变量写入配置文件,可以确保在系统重启或用户重新登录后环境变量依然有效。

(1)安装OpenJDK 11。

[root@localhost ~]# dnf -y install java-11-openjdk java-11-openjdk-devel ​ 正在更新 Subscription Management 软件仓库。 无法读取客户身份 ​ 本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。 ​ AppSt 0.0 B/s |   0 B     00:00     Errors during downloading metadata for repository ‘AppStream’:  - Curl error (37): Couldn‘t read a file:// file for file:///mnt/AppStream/repodata/repomd.xml [Couldn’t open file /mnt/AppStream/repodata/repomd.xml] 错误:为仓库 ‘AppStream’ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried 

(2)创建Java环境变量脚本/etc/profile.d/java.sh

讯享网[root@localhost ~]# cat &gt; /etc/profile.d/java.sh &lt;&lt;‘EOF’ &gt; export JAVA_HOME=\((dirname \)(dirname \((readlink \)(readlink \((which java))))) &gt; export PATH&#61;\)PATH:$JAVA_HOME/bin

(3)执行source命令,使Java环境变量生效。

[root@localhost ~]# source /etc/profile.d/java.sh 


小讯
上一篇 2025-04-29 07:57
下一篇 2025-05-12 15:50

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/196736.html