项目一 项目实训(初识Shell脚本)
文章目录
实训任务:通过编写简单的shell脚步,完成使用数据输入输出命令获取用户输入,将脚本输出写入文件,以及重定向脚本的输入输出操作,并通过设置变量存储不同类型的数据,以便在脚本中处理数据。
实训目的:
(1)掌握shell脚本的基本格式。
(2)掌握输入输出重定向和管道符的使用方法。
(3)掌握数据输入输出命令的使用方法。
(4)掌握变量的定义和调用方法。
( 5)掌握运算符的使用方法。
【实训内容】
(1)使用输入输出重定向,将Shell脚本的输出重定向到文件。
(2)编写shell脚本,使用echo、printf、read命令实现数据的输入输出。
(3)编写shell脚本,定义和调用变量,并在配置文件中永久设置环境变量。
(4)编写shell脚本,进行算数运算
【实训环境】
在进行本项目的实训操作前,提前准备好Linux操作系统环境,RHEL、CentOS Stream、Debian、Ubuntu、华为openEuler、麒麟openKylin等常见Linux发行版本都可以进行项目实训。
任务一 输入输出重定向
1.任务描述
编写简单的shell脚步,使用输入输出重定向及管道符将脚本的信息重定向到文件。
2.任务实施
(1)创建shell脚本firstscript.sh,使用vim文本编辑器在用户家目录下创建一个新的文本文件,将其命名为firstscript.sh,插入以下文本并保存文件,将输入重定向到文件中。
[root@localhost ~]#
[root@localhost ~]# vim input.txt
[root@localhost ~]# cat input.txt
2021
2022
2023
2024
2035
2025
1999
2000
2000
[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 firstscript.sh
1999
2000
2000
2021
2022
2023
2024
2025
2035
[root@localhost ~]# cat input.txt
2021
2022
2023
2024
2035
2025
1999
2000
2000
[root@localhost ~]#
(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@localhost ~]# vim file.sh
[root@localhost ~]# cat file.sh
#!/bin/bash
# 忽略read命令的输入
read -p "Enter your name:" name < /dev/null
echo "Your name is : $name"
[root@localhost ~]#
(7)从标准输入中读取多行文本。
[root@localhost ~]# vim file.sh
[root@localhost ~]# cat file.sh
#!/bin/bash
# 忽略read命令的输入
#!/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
[root@localhost ~]#
(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
[root@localhost ~]#
(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
[root@localhost ~]#
任务二 数据输入输出操作
1.任务描述
编写shell脚本,通过数据输入输出与用户交互,使用户输入数据或输出信息;通过数据输入输出读取文件中的数据或写入数据到文件中;通过数据输入输出可以与其他程序或系统交互,以获取或输出数据。使用read命令读取用户输入的数据,使用echo或printf命令输出信息。
2 任务实施
(1)使用read命令读取用户输入的数据。
[root@localhost ~]# vim printf0l.sh
[root@localhost ~]# cat printf0l.sh
#!/bin/bash
# 读入用户输入的数字
read -p "Enter a number:" num
# 输出用户输入的数字
echo "You entered: $num"
# 执行脚本并查看输出结果
[root@localhost ~]# chmod +x printf0l.sh
[root@localhost ~]# bash printf0l.sh
Enter a number:100
You entered: 100
[root@localhost ~]#
(2)使用read命令读取多个数据。
[root@localhost ~]# bash printf02.sh
Enter your name,age and gender: Amy 19 female
Name: Amy
Age: 19
Gender: female
[root@localhost ~]#
(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
[root@localhost ~]#
(4)使用printf命令格式化输出数字。
[root@localhost ~]# vim printf04.sh
[root@localhost ~]# cat printf04.sh
#!/bin/bash
printf "%.2f\n" 3.14159265
printf "%.4f\n" 3.14159265
printf "%d\n" 123456
printf "x\n"255
# 执行脚本并查看输出结果
[root@localhost ~]# chmod +x printf04.sh
[root@localhost ~]# bash printf04.sh
3.14
3.1416
123456
ff
[root@localhost ~]#
(5)使用printf命令格式化输出字符串。
[root@localhost ~]# vim printf05.sh
[root@localhost ~]# cat printf05.sh
#!/bin/bash
printf "%-10s %-8s %-4s\n" Name Gender Age
printf "%-10s %-8s %-4d\n" John Male 30
printf "%-10s %-8s %-4d\n" Mary Female 25
# 执行脚本并查看输出结果
[root@localhost ~]# bash printf05.sh
Name Gender Age
John Male 30
Mary Female 25
[root@localhost ~]#
(6)使用printf命令输出多个字符和字符串。
[root@localhost ~]# vim printf06.sh
[root@localhost ~]# cat printf06.sh
#!/bin/bash
printf "%s %s %s\n" A B C
printf "%s %s %s\n" A B C D E F G
# 定义字符变量
char='a'
# 使用%c格式化输出字符
printf "The character is %c.\n” $char
# 定义字符串
string="Hello,World!"
# 使用%s格式化输出字符串
printf "The string is %s.n" "$string"
# 使用%-10s格式化输出左对齐的字符串
printf "%-10s\n" "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.\n" "$name" "$age"
# 上面的脚本中定义两个变量:name 和 age
# 使用printf命令输出字符串,并使用变量时需要使用$调用变量
# 执行脚本并查看输出结果
[root@localhost ~]# chmod +x printf07.sh
[root@localhost ~]# bash printf07.sh
My name is John,and I am 30 years old.
[root@localhost ~]#
任务三 shell变量操作
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
awk: 命令行:1: NR==2{printf "Total: %sMB, Used: %sMB, Free: %sMB ,$2, $3, $4}
awk: 命令行:1: ^ 未结束的字符串
awk: 命令行:1: NR==2{printf "Total: %sMB, Used: %sMB, Free: %sMB ,$2, $3, $4}
awk: 命令行:1: ^ syntax error
Memory:
IP: 192.168.77.129
172.17.0.1
CPU: 0.0%
[root@localhost ~]#
(2)编写shell脚本,输出$0、$1、$2、 3 、 3、 3、@、KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲、!、 ? 、 ?、 ?、*、$$等位置参数的变量信息。
[root@localhost ~]# vim location-output.sh
[root@localhost ~]# cat location-outout.提示
!/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@localhost ~]# chomd +x location-output.sh
[root@localhost ~]# 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: 3217
Name is:one two three
[root@localhost ~]#
(3)输出当前目的的日历信息,并使用printf命令格式化输出。
[root@localhost ~]# touch fjj.sh
[root@localhost ~]# vi fjj.sh
[root@localhost ~]# bash fjj.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
[root@localhost ~]#
(4)编写shell脚本,使用变量并结合printf命令格式化输出当前系统的磁盘分区、swap分区、逻辑卷等信息。
[root@localhost ~]# vim disk-output.sh
[root@localhost ~]# cat disk-output.sh
#!/bin/bash
# 使用df命令获取磁盘分区信息
disk_partitions=$(df -h)
# 使用swapon命令来获取逻辑卷信息
logical_volumes=$(lvdisplay)
# 使用printf 命令格式化输出
printf "Disk Partitions:\n\n"
printf "%s\n""$disk_partitions"
printf "\nSwap Partitions:\n\n"
printf "%s\n" "$swap_partitions"
printf "\nLogical Volumes:\n\n"
printf "%s\n""$logical_volumes"
# 执行脚本并查看结果
[root@localhost ~]# chmod +x disk-output.sh
[root@localhost ~]# bash disk-output.sh
Disk Partitions:
disk-output.sh: 第 9 行:printf: “�”:无效格式字符
文件系统 容量 已用 可用 已用
Swap Partitions:
Logical Volumes:
--- Logical volume ---
LV Path /dev/rhel/swap
LV Name swap
VG Name rhel
LV UUID NeHE2b-tCbZ-yhPw-VRjs-Odg8-QCc9-bnKfoF
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2024-03-13 20:30:19 +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 eWLtBN-5vaS-5LNf-G0vr-yvto-VALG-zCiLiB
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2024-03-13 20:30:19 +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
[root@localhost ~]#
任务四 算术运算符操作
1.任务描述
在shell中,算术运算主要用于执行各种数学计算。常见的算术运算包括加法、减法、乘法、除法、求余数、幂运算等。
2.任务实施
(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
#定义圆的半径
circle_radius=10
# 计算圆的面积
circle_area=$(echo "scale=2; 3.14 * $circle_radius * $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 ~]# chomd +x calculate01.sh
bash: chomd: command not found...
Similar command is: 'chmod'
[root@localhost ~]# chmod +x calculate01.sh
[root@localhost ~]# bash calculate01.sh
三角形面积:24.00
圆的面积:314.00
圆的周长:62.80
(2)编写shell脚本,使用bc命令进行算术运算。
[root@localhost ~]# vim calculate02.sh
[root@localhost ~]# cat calculate02.sh
#!/bin/bash
# 定义变量
a=10
b=20
c=30
# 使用bc命令进行算术运算
result1=$(echo "$a + $b"| bc)
result2=$(echo "$a * $b"| bc)
result3=$(echo "scale=2; $a / $b"| bc)
result4=$(echo "$c % $a"| bc)
result5=$(echo "scale=3; $a ^ $b"| bc)
result6=$(echo "$a + $b * $c"| bc)
result7=$(echo "$a * $b + $c"| bc)
result8=$(echo "$c % $a + $b"| bc)
# 输出结果
echo "a + b = $result1"
echo "a * b = $result2"
echo "a / b = $result3"
echo "c % a = $result4"
echo "a ^ b = $result5"
echo "a + b * c = $result6"
echo "a * b + c = $result7"
echo "c % a + b = $desult8"
# 执行脚本并查看输出结果
[root@localhost ~]# bash calculate02.sh
a + b = 30
a * b = 200
a / b = .50
c % a = 0
a ^ b = 100000000000000000000
a + b * c = 610
a * b + c = 230
c % a + b =
[root@localhost ~]#
(3)编写shell脚本,使用let命令进行算术运算。
[root@localhost ~]# vim calculate03.sh
[root@localhost ~]# 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=a**b
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 calculate03.sh
[root@localhost ~]# bash calculate03.sh
a + b = 30
a * b = 200
c % a = 0
a ^ b = 7766279631452241920
a + b * c = 610
a * b + c = 230
c % a + b = 20
[root@localhost ~]#
(4)编写shell脚本,使用expr命令进行算术运算。
[root@localhost ~]# vim calculate04.sh
[root@localhost ~]# cat calculate04.sh
#!/bin/bash
# 定义变量
a=10
b=20
c=30
# 使用expr命令进行算术运算
result1=`expr $a + $b`
result2=`expr $a \* $b`
result3=`expr $c % $a`
result4=`expr $a \* $a \* $a`
result5=`expr $a + $b \* $c`
result6=`expr $a \* $b + $c`
result7=`expr $c % $a + $b`
# 输出结果
echo "a + b = $result1"
echo "a * b = $result2"
echo "c % a = $result3"
echo "a ^ 3 = $result4"
echo "a + b * c = $result5"
echo "a * b + c = $result6"
echo "c % a + b = $result7"
# 执行脚本并查看输出结果
[root@localhost ~]# chmod +x calculate04.sh
[root@localhost ~]# bash calculate04.sh
a + b = 30
a * b = 200
c % a = 0
a ^ 3 = 1000
a + b * c = 610
a * b + c = 230
c % a + b = 20
[root@localhost ~]#
(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 = 7766279631452241920
a + b * c = 610
a * b + c = 230
c % a + b =20
[root@localhost ~]#
任务五 设置环境变量
1.任务描述
在Linux操作系统中,设置java环境变量涉及JAVA_HOME和PATH两个主要的环境变量,将环境变量写入配置文件,可以确保在系统重启或用户重新登录后环境变量依然有效。
2.任务实施
(1)安装OpenJDK 11。
[root@localhost ~]# dnf -y install java-11-openjdk java-11-openjdk-devel
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册
=======================================================================================================
软件包 架构 版本 仓库 大小
=======================================================================================================
安装:
java-11-openjdk x86_64 1:11.0.20.0.8-3.el9 AppStream 441 k
java-11-openjdk-devel x86_64 1:11.0.20.0.8-3.el9 AppStream 3.3 M
安装依赖关系:
copy-jdk-configs noarch 4.0-3.el9 AppStream 29 k
java-11-openjdk-headless x86_64 1:11.0.20.0.8-3.el9 AppStream 40 M
javapackages-filesystem noarch 6.0.0-4.el9 AppStream 17 k
lksctp-tools x86_64 1.0.19-2.el9 BaseOS 98 k
lua x86_64 5.4.4-4.el9 AppStream 192 k
lua-posix x86_64 35.0-8.el9 AppStream 155 k
mkfontscale x86_64 1.2.1-3.el9 AppStream 34 k
ttmkfdir x86_64 3.0.9-65.el9 AppStream 55 k
tzdata-java noarch 2023c-1.el9 AppStream 234 k
xorg-x11-fonts-Type1 noarch 7.5-33.el9 AppStream 509 k
事务概要
=======================================================================================================
安装 12 软件包
总计:45 M
安装大小:187 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
运行脚本: copy-jdk-configs-4.0-3.el9.noarch 1/1
运行脚本: java-11-openjdk-headless-1:11.0.20.0.8-3.el9.x86_64 1/1
准备中 : 1/1
安装 : lksctp-tools-1.0.19-2.el9.x86_64 1/12
安装 : tzdata-java-2023c-1.el9.noarch 2/12
安装 : ttmkfdir-3.0.9-65.el9.x86_64 3/12
安装 : mkfontscale-1.2.1-3.el9.x86_64 4/12
安装 : xorg-x11-fonts-Type1-7.5-33.el9.noarch 5/12
运行脚本: xorg-x11-fonts-Type1-7.5-33.el9.noarch 5/12
安装 : lua-posix-35.0-8.el9.x86_64 6/12
安装 : lua-5.4.4-4.el9.x86_64 7/12
安装 : copy-jdk-configs-4.0-3.el9.noarch 8/12
安装 : javapackages-filesystem-6.0.0-4.el9.noarch 9/12
安装 : java-11-openjdk-headless-1:11.0.20.0.8-3.el9.x86_64 10/12
运行脚本: java-11-openjdk-headless-1:11.0.20.0.8-3.el9.x86_64 10/12
安装 : java-11-openjdk-1:11.0.20.0.8-3.el9.x86_64 11/12
运行脚本: java-11-openjdk-1:11.0.20.0.8-3.el9.x86_64 11/12
安装 : java-11-openjdk-devel-1:11.0.20.0.8-3.el9.x86_64 12/12
运行脚本: java-11-openjdk-devel-1:11.0.20.0.8-3.el9.x86_64 12/12
运行脚本: copy-jdk-configs-4.0-3.el9.noarch 12/12
运行脚本: java-11-openjdk-headless-1:11.0.20.0.8-3.el9.x86_64 12/12
运行脚本: java-11-openjdk-1:11.0.20.0.8-3.el9.x86_64 12/12
运行脚本: java-11-openjdk-devel-1:11.0.20.0.8-3.el9.x86_64 12/12
验证 : copy-jdk-configs-4.0-3.el9.noarch 1/12
验证 : java-11-openjdk-1:11.0.20.0.8-3.el9.x86_64 2/12
验证 : java-11-openjdk-devel-1:11.0.20.0.8-3.el9.x86_64 3/12
验证 : java-11-openjdk-headless-1:11.0.20.0.8-3.el9.x86_64 4/12
验证 : javapackages-filesystem-6.0.0-4.el9.noarch 5/12
验证 : lua-5.4.4-4.el9.x86_64 6/12
验证 : lua-posix-35.0-8.el9.x86_64 7/12
验证 : mkfontscale-1.2.1-3.el9.x86_64 8/12
验证 : ttmkfdir-3.0.9-65.el9.x86_64 9/12
验证 : tzdata-java-2023c-1.el9.noarch 10/12
验证 : xorg-x11-fonts-Type1-7.5-33.el9.noarch 11/12
验证 : lksctp-tools-1.0.19-2.el9.x86_64 12/12
已更新安装的产品。
已安装:
copy-jdk-configs-4.0-3.el9.noarch java-11-openjdk-1:11.0.20.0.8-3.el9.x86_64
java-11-openjdk-devel-1:11.0.20.0.8-3.el9.x86_64 java-11-openjdk-headless-1:11.0.20.0.8-3.el9.x86_64
javapackages-filesystem-6.0.0-4.el9.noarch lksctp-tools-1.0.19-2.el9.x86_64
lua-5.4.4-4.el9.x86_64 lua-posix-35.0-8.el9.x86_64
mkfontscale-1.2.1-3.el9.x86_64 ttmkfdir-3.0.9-65.el9.x86_64
tzdata-java-2023c-1.el9.noarch xorg-x11-fonts-Type1-7.5-33.el9.noarch
完毕!
[root@localhost01 ~]#
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.youkuaiyun.com/qq_65998623/article/details/142578590
(2)创建Java环境变量脚本/etc/profile.d/java.sh。
[root@localhost ~]# cat > /etc/profile.d/java.sh。
export JAVA_HOME=$(dirname $(dirname $(dirname $readlink $(readlink $(which java)))))
export PATH=$PATH:$JAVA_HOME/bin
> EOF
(3)执行source命令,使Java环境变量生效。
[root@localhost ~]# source /etc/profile.d/java.sh