判断



在这里插入图片描述


一、条件测试

格式

格式1:test 条件表达式
格式2:[ 条件表达式 ]
格式3:[[ 条件表达式 ]]
注意:[] 中的条件表达式两边需要空格

1、字符串比较
[“字符串”=“字符串”]

=,==( 等于


[root@nfs 桌面]# [ "$USER" = "root" ];echo $?
0
[root@nfs 桌面]# [ "$USER" == "root" ];echo $?
0
[root@nfs ~]# su - guan一次登录:五 1230 14:33:12 CST 2022pts/0 上
[guan@nfs ~]$ [ "$USER" = "root" ];echo $?
1
[guan@nfs ~]$ [ "$USER" = "guan" ];echo $?
0



!=( 不等于

[root@nfs ~]# [ "$USER" = "root" ];echo $?
0
[root@nfs ~]# [ "$USER" != "guan" ];echo $?
0

[root@nfs ~]# [ "$USER" = "guan" ];echo $?
1

-z:判断字符串长度是为0

[root@web1 ~]# var1=123
[root@web1 ~]# [ -z "$var1" ];echo $?
1
[root@web1 ~]# var2=
[root@web1 ~]# [ -z "$var2" ];echo $?
0

-n:判断字符串长度不是为0

[root@nfs ~]# var1=123
[root@nfs ~]# var2=
[root@nfs ~]# [ -n "$var1" ];echo $?
0
[root@nfs ~]# [ -n "$var2" ];echo $?
1

例子

[root@nfs 桌面]# vim test04.sh
[root@nfs 桌面]# bash test04.sh输入你的选择[yes/no]yes
hello
[root@nfs 桌面]# bash test04.sh输入你的选择[yes/no]no
heihei
[root@nfs 桌面]# cat test04.sh
#!/bin/bash

read -p "请输入你的选择:[yes/no]" select

if  [ $select = "yes" ]
then
echo hello
else
echo heihei
fi

2、文件测试
[ 操作符 文件或目录 ]

[root@nfs ~]# ls /test
ls: 无法访问/test: 没有那个文件或目录
[root@nfs ~]# [ -d /test ];echo $?
1
[root@nfs ~]# [ -d /home ];echo $?
0
[root@nfs 桌面]# test -d /home   //-d 后跟一个目录
[root@nfs 桌面]# test -d /home;echo $?
0

[root@nfs 桌面]# ls
2022-12-28_file1.txt          ping.sh    VMwareTools-10.3.10-13959562.tar.gz
2022-12-28_file2.txt          test01.sh  vmware-tools-distrib
bash.sh                       test02.sh  yum.sh
file1                         test03.sh  zabbix-6.0.9.tar.gz
nginx-1.22.0                  test04.sh  zabbix.shell
nginx-1.22.0.tar.gz           test05.sh
percona-xtrabackup24.rpm.zip  test.sh
[root@nfs 桌面]# [ -f nginx-1.22.0 ];echo $?
1
[root@nfs 桌面]# [ -f 2022-12-28_file1.txt ];echo $?
0
[root@nfs 桌面]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2
[root@nfs 桌面]# ls /dev/sd* -l
brw-rw---- 1 root disk 8, 0 1228 21:28 /dev/sda
brw-rw---- 1 root disk 8, 1 1228 21:28 /dev/sda1
brw-rw---- 1 root disk 8, 2 1228 21:28 /dev/sda2
[root@nfs 桌面]# file /dev/sda
/dev/sda: block special
[root@nfs 桌面]# [ -b /dev/sda  ];echo $?
0

[root@nfs 桌面]# ls -l file1
-rw-r--r-- 1 root root 78 1228 18:22 file1
[root@nfs 桌面]# chmod u+s file1
[root@nfs 桌面]# ls -l file1
-rwSr--r-- 1 root root 78 1228 18:22 file1
[root@nfs 桌面]# [ -u file1  ];echo $?
0
[root@nfs 桌面]# chmod u-s file1
[root@nfs 桌面]# ls -l file1
-rw-r--r-- 1 root root 78 1228 18:22 file1
[root@nfs 桌面]# [ -u file1  ];echo $?
1

-f filename filename 存在返回
-b filename 当filename 存在并且是块文件时返回真(返回0)
d pathname pathname存在并且是一个目录时返回
-h filename 当filename 存在并且是符号链接文件时返回真(或 -L filename)
-u pathname: pathname指定的文件或目录存在并且设置了SUID位时返回
c filename: 当filename 存在并且是字符文件时返回
-e pathname: 当由pathname 指定的文件或目录存在时返回
g pathname: 当由pathname 指定的文件或者目录存在并且设置了SGID位时返回真
k pathname: 当由pathname 指定的文件或者目录存在并且设置了”粘滞” 位时返回真
-p filename: 当filename 存在并且是命名管道时返回真
-r pathname: 当由pathname指定的文件或目录存在并且可读时返回真
-s filename: 当filename 存在并且文件大小大于0时返回真
-S filename: 当filename 存在并且时socket 时返回真
-t fd 当fd时与终端设备相关联的文件描述符时返回真
-w pathname: 当由pathname 指定的文件或目录存在并且返回真时
-x pathname: 当由pathname 指定的文件或目录存在并且可执行时返回真
-O pathname: 当由pathname 存在并且被当前进程的有效用户id的用户拥有时返回真(字母大写O)
-G pathname: 当由pathname 存在并且属于当前进程的有效用户id的用户的用户组时返回真
file1 -nt file2: file1 比 file2 新时返回真

例子

[root@nfs 桌面]# vim test05.sh
[root@nfs 桌面]# bash test05.sh输入安装的目录:/test
你输入的目录不存在,开始创建。
你的目录 /test ,已创建程序已经开始安装
[root@nfs 桌面]# bash test05.sh
请输入安装的目录:/test
/test 目录已存在,安装程序开始 
[root@nfs 桌面]# 
[root@nfs 桌面]# ls /
backup  boot   dev  home  lib64  mnt  proc  run   srv  test  usr  webdata
bin     b.org  etc  lib   media  opt  root  sbin  sys  tmp   var  webroot

[root@nfs 桌面]# cd /test
[root@nfs test]# 


[root@nfs 桌面]# cat test05.sh
#!/bin/bash
read -p "请输入安装的目录:" dir
if [ -d $dir ]
	then
		echo "$dir 目录已存在,安装程序开始 "
	else
		echo "你输入的目录不存在,开始创建。"
		mkdir $dir
		echo "你的目录 $dir ,已创建。"
		echo "程序已经开始安装"

fi
[root@nfs 桌面]# 


3、数值比较
[ 整数1 操作符 整数2 ]

[root@nfs 桌面]# id -u
0
[root@nfs 桌面]# su -guan
su: group uan does not exist
[root@nfs 桌面]# su - guan一次登录:五 1230 14:45:57 CST 2022pts/0 上
[guan@nfs ~]$ id -u  //id -u 打印这个用户的id号
1001    
[guan@nfs ~]$ id --help
Usage: id [OPTION]... [USER]
Print user and group information for the specified USER,
or (when USER omitted) for the current user.

  -a             ignore, for compatibility with other versions
  -Z, --context  print only the security context of the current user
  -g, --group    print only the effective group ID
  -G, --groups   print all group IDs
  -n, --name     print a name instead of a number, for -ugG
  -r, --real     print the real ID instead of the effective ID, with -ugG
  -u, --user     print only the effective user ID
  -z, --zero     delimit entries with NUL characters, not whitespace;
                   not permitted in default format
      --help		显示此帮助信息退出
      --version		显示版本信息退出

如果不附带任何选项程序显示一些可供识别用户身份的有用信息。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告id 的翻译错误获取完整文档,请运行info coreutils 'id invocation'

操作符

[root@nfs test]# [ 1 -gt 2 ];echo $?  //大于
1
[root@nfs test]# [ 1 -lt 2 ];echo $?  //小于
0
[root@nfs test]# [ 1 -eq 2 ];echo $?  //等于
1
[root@nfs test]# [ 1 -ne 2 ];echo $?  //不等于
0
[root@nfs test]# [ 1 -ge 2 ];echo $?  //大于等于
1
[root@nfs test]# [ 1 -le 2 ];echo $?  //小于等于
0

C语言风格

[root@nfs test]# ((1<2));echo $?
0
[root@nfs test]# ((1>2));echo $?
1
[root@nfs test]# ((1==2));echo $?
1
[root@nfs test]# ((1>=2));echo $?
1
[root@nfs test]# ((1<=2));echo $?
0
[root@nfs test]# ((1!=2));echo $?
0

[root@nfs test]# vim test07.sh
[root@nfs test]# bash test07.sh
please input your score:59
不及格
[root@nfs test]# bash test07.sh
please input your score:100
及格
[root@nfs test]# cat test07.sh
#!/bin/bash
read -p "please input your score:" num
if [ $num -ge 60 ]
	then
		echo "及格"
	else
		echo "不及格"
fi


例子

[root@nfs 桌面]# vim test06.sh
[root@nfs 桌面]# bash test06.sh
admin
[root@nfs 桌面]# cp test06.sh /home/guan
[root@nfs 桌面]# su - guan一次登录:五 1230 16:36:46 CST 2022pts/0 上
[guan@nfs ~]$ ls
test06.sh
[guan@nfs ~]$ bash test06.sh 
user
[guan@nfs ~]$ su - root
密码:
上一次登录:五 1230 16:40:09 CST 2022pts/0 上
[root@nfs ~]# cd 桌面

[root@nfs 桌面]# cat test06.sh
#!/bin/bash
num=$(id -u)
if [ $num -eq 0 ]
	then
		echo "admin"
	else
		echo "user"
fi

and 和 or
逻辑的(and) 与 (or)
&amp;&amp; 逻辑的 AND 的意思,-a也是这个意思,两个条件同时成立为真。
|| 逻辑的OR的意思, -o也是这个意思,两个条件一个成立为真。

例子

[root@nfs test]# num=61;[ $num -gt 60 -a $num -lt 80 ];echo $?
0
[root@nfs test]# num=59;[ $num -gt 60 -a $num -lt 80 ];echo $?
1
[root@nfs test]# num=59;[ $num -gt 60 -o $num -lt 80 ];echo $?
0

多种表达式

[root@nfs test]# [ 1 -lt 2 -a 5 -gt 10 ];echo $?
1
[root@nfs test]# [ 1 -lt 2 -o 5 -gt 10 ];echo $?
0

二、流程控制

1.单分支结构

语法

if  条件测试
then 命令序列
fi

例子
需求编写一个脚本,由用户输入用户名,如果用户名不存在,则创建该用户,并设置密码为123456

[root@nfs test]# vim test08.sh
[root@nfs test]# bash test08.sh
please input a username:tom
123456 | passwd --stdin tom &amp;> /dev/null
tom create finished,the password is 123456
[root@nfs test]# id tom
uid=1006(tom) gid=1006(tom)=1006(tom)
[root@nfs test]# cat test08.sh
#!/bin/bash
read -p "please input a username:" name

id $name &amp;> /dev/null

if [ $? -ne 0 ];then
	useradd $name
	echo "123456 | passwd --stdin $name &amp;> /dev/null"
	echo "$name create finished,the password is 123456"
fi

[root@nfs test]# id guan
uid=1001(guan) gid=1001(guan)=1001(guan)
[root@nfs test]# bash test09.sh
please input a username:guan   //该用户已存在,所以不会进行创建
[root@nfs test]# bash test09.sh
please input a username:lihua
123456 | passwd --stdin lihua &amp;> /dev/null
lihua create finished,the password is 123456
[root@nfs test]# cat test09.sh
#!/bin/bash
read -p "please input a username:" name

if ! id $name &amp;> /dev/null
	then
	useradd $name
	echo "123456 | passwd --stdin $name &amp;> /dev/null"
	echo "$name create finished,the password is 123456"
fi


2.双分支结构

语法

if  条件测试
then 命令序列
else 
fi

例子1
需求编写一个脚本,由用户输入用户名,如果用户名不存在,则创建该用户,并设置密码为123456;否则,提示用户已存在

[root@nfs test]# vim test10.sh
[root@nfs test]# bash test10.sh
please input a username:tom
tom already exist!
[root@nfs test]# userdel -r tom
[root@nfs test]# bash test10.sh
please input a username:tom
123456 | passwd --stdin tom &amp;> /dev/null
tom create finished,the password is 123456
[root@nfs test]# cat test10.sh
#!/bin/bash
read -p "please input a username:" name

if ! id $name &amp;> /dev/null
	then
	useradd $name
	echo "123456 | passwd --stdin $name &> /dev/null"
	echo "$name create finished,the password is 123456"
	else
	echo "$name already exist!"
fi

[root@nfs test]# 

例子2
需求编写一个脚本,由用户输入用户名,判断该用户的uidgid,如果相同,则显示Good user;否则显示 Bad user。

[root@nfs test]# vim test11.sh
[root@nfs test]# id -u guan
1001
[root@nfs test]# id -g guan
1001
[root@nfs test]# id  guan
uid=1001(guan) gid=1001(guan)=1001(guan)
[root@nfs test]# bash test11.sh
please input username:guan
Good user.
[root@nfs test]# usermod -g 1008 guan
[root@nfs test]# id  guan
uid=1001(guan) gid=1008(tom)=1008(tom)
[root@nfs test]# bash test11.sh
please input username:guan
test11.sh:行9: echoBad user.
[root@nfs test]# cat test11.sh
#!/bin/bash
read -p "please input username:" name
user_id=`id -u $name`
group_id=`id -g $name`

if [ $user_id -eq $group_id ];then
	echo "Good user."
else
	echo "Bad user."
fi

3.多分支结构

语法

if  条件测试1
then 命令序列
elif 条件测试2
then 命令序列
elif 条件测试3
then 命令序列
else 命令序列
fi

[root@nfs test]# vim test12.sh
[root@nfs test]# bash test12.sh
This is night
[root@nfs test]# date +%H
19
[root@nfs test]# cat test12.sh
#!/bin/bash
#姓名
#时间
#目的
hour=`date +%H`

if [ $hour -ge 6 -a $hour -le 10 ];then
	echo "This is morning"
elif [ $hour -ge 11 -a $hour -le 13 ];then
	echo "This is noon"
elif [ $hour -ge 13 -a $hour -le 18 ];then
	echo "This is afternoon"
else
	echo "This is night"
fi

4.嵌套结构

语法

if  条件测试 then 命令序列
		if 条件测试 then 命令序列
		else  命令序列
		fi
else 命令序列
fi

[root@nfs test]# vim test13.sh
[root@nfs test]# bash test13.sh
please input username: luo
please input your new user password:guan123456
guan123456 | passwd --stdin luo &> /dev/null
luo create finished,password is guan123456 
[root@nfs test]# cat test13.sh
#!/bin/bash
read -p "please input username: " name
id $name &> /dev/null

if [ $? -eq 0 ];then
	echo "$name already exist!"
	
	else
	
	read -p "please input your new user password:" pass
	
	if [ ${#pass} -ge 7 ];then
	
		useradd $name
		echo "$pass | passwd --stdin $name &> /dev/null"
		echo "$name create finished,password is $pass "
	else
		echo "user is not created,beause password is unqualified!"
	fi
fi


[root@nfs test]# bash test13.sh
please input username: luo
luo already exist!
[root@nfs test]# bash test13.sh
please input username: abc
please input your new user password:123
user is not created,beause password is unqualified!

三、匹配模式

1.case 语法结构

case 变量 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
模式3)
命令序列3
;;
*)匹配后命令序列
esac

例子1

[root@nfs test]# vim test14.sh
[root@nfs test]# bash test14.sh
请输入你的操作系统linux
windows

[root@nfs test]# bash test14.sh
请输入你的操作系统ubuntu
other...
[root@nfs test]# cat test14.sh
#!/bin/bash
read -p "请输入你的操作系统:" str
case $str in
windows)
echo "linux"
;;
linux)
echo "windows"
;;
*)
echo "other..."
esac


#read -p "请输入你的操作系统:" str
#if [ $str = windows ];then
#	echo "linux"
#elif [ $str = linux ];then
#	echo "windows"
#else
#	echo "other..."
#fi


例子2

[root@nfs test]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@nfs test]# cat /etc/redhat-release |awk '{print $4}'
7.6.1810
[root@nfs test]# cat /etc/redhat-release |awk '{print $4}' | awk -F"." '{print $1}'
7
[root@nfs test]# cat test15.sh
#!/bin/bash
#localyum

os_version=`cat /etc/redhat-release |awk '{print $4}' | awk -F"." '{print $1}'`
if [ "$os_version" = "7" ];then
	cat > /etc/yum.repos.d/centos7.repo <<-EOF
	[centos]
	name=centos7
	baseurl=ftp://192.168.200.182/contos7u3
	gpgcheck=0
	EOF	
	
	yum makecache
	yum repolist
	echo "yum configuration completed!"
fi
if [ "$os_version" = "6" ];then
        cat > /etc/yum.repos.d/centos6.repo <<-EOF
        [centos]
        name=centos6
        baseurl=ftp://192.168.200.182/contos6u3
        gpgcheck=0
        EOF     

        yum makecache
        yum repolist
        echo "yum configuration completed!"
fi
if [ "$os_version" = "5" ];then
        cat > /etc/yum.repos.d/centos5.repo <<-EOF
        [centos]
        name=centos5
        baseurl=ftp://192.168.200.182/contos5u3
        gpgcheck=0
        EOF     

        yum makecache
        yum repolist
        echo "yum configuration completed!"
fi

[root@nfs test]# vim test16.sh
[root@nfs test]# cat test16.sh
#!/bin/bash
#localyum

os_version=`cat /etc/redhat-release |awk '{print $4}' | awk -F"." '{print $1}'`
case "$os_version" in 
"7")
cat > /etc/yum.repos.d/centos7.repo <<-EOF
	[centos]
	name=centos7
	baseurl=ftp://192.168.200.182/contos7u3
	gpgcheck=0
	EOF	
	
	yum makecache
	yum repolist
	echo "yum configuration completed!"
	;;
"6")
cat > /etc/yum.repos.d/centos6.repo <<-EOF
        [centos]
        name=centos6
        baseurl=ftp://192.168.200.182/contos6u3
        gpgcheck=0
        EOF     
        
        yum makecache
        yum repolist
        echo "yum configuration completed!"
	;;
"5")
cat > /etc/yum.repos.d/centos5.repo <<-EOF
        [centos]
        name=centos5
        baseurl=ftp://192.168.200.182/contos5u3
        gpgcheck=0
        EOF     
        
        yum makecache
        yum repolist
        echo "yum configuration completed!"
	;;
*)
	echo "没有找到对应版本"
esac
[root@nfs test]# 

例子3

[root@nfs test]# ls /home
guan  Guan  guanguan  Guanguan  lihua  luo  tom  userA  userC

[root@nfs test]# vim test18.sh
[root@nfs test]# bash test18.sh
please input username:lihua
are you sure?[yes/no]:yes
lihua is deleted!
[root@nfs test]# ls /home
guan  Guan  guanguan  Guanguan  luo  userA  userC


[root@nfs test]# vim test18.sh
[root@nfs test]# cat test18.sh
#!/bin/bash
#1请输入删除用户名
read -p "please input username:" user
#2输出用户名ID
id $user &> /dev/null
#3判断用户是否存在
if [ $? -ne 0 ];then
	echo "no such user: $user"
	exit 1
fi
#4请用户确认是否删除
read -p "are you sure?[yes/no]:" action
case "$action" in 
Y|y|YES|yes)
userdel -r $user
echo "$user is deleted!"
;;
*)
echo "thank you"
;;
esac

[root@nfs test]# cat test17.sh
#!/bin/bash
#name
#time
#1请输入删除用户名
read -p "please input a username :" uesr
#2输出用户名ID
id $user &> /dev/null
#4判断用户是否存在
if [ $? -ne 0 ];then
	echo "no such user: $user"
	exit 1
fi
#3请用户确认是否删除 
read -p "are you sure?[yes/no]: " action
if [ "$action" = "yes" -o "$action" = "y" -o "$action" = "Y" ];then
	userdel -r  $user
	echo "$user is deleted!"
else
	echo "thank you"
fi

例子4

[root@nfs test]# bash systemmanage.sh
h-help
f-disk partation
d-filesystem mount
m-memory
u-system load
q-exit
please input ( h for help):h
error
h-help
f-disk partation
d-filesystem mount
m-memory
u-system load
q-exit
please input ( h for help):m
              total        used        free      shared  buff/cache   available
Mem:           1819         891          85          83         842         629
Swap:          2047          16        2031
h-help
f-disk partation
d-filesystem mount
m-memory
u-system load
q-exit
please input ( h for help):d
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        17G  7.4G  9.7G   44% /
devtmpfs                devtmpfs  894M     0  894M    0% /dev
tmpfs                   tmpfs     910M     0  910M    0% /dev/shm
tmpfs                   tmpfs     910M   11M  900M    2% /run
tmpfs                   tmpfs     910M     0  910M    0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  179M  836M   18% /boot
tmpfs                   tmpfs     182M   28K  182M    1% /run/user/0
h-help
f-disk partation
d-filesystem mount
m-memory
u-system load
q-exit
please input ( h for help):q
[root@nfs test]# cat systemmanage.sh
#!/bin/bash
#
while :
do

cat <<-EOF
h-help
f-disk partation
d-filesystem mount
m-memory
u-system load
q-exit
EOF

#
read -p "please input ( h for help):" action
case "$action" in
f)
	fdisk -l
;;
d)
	df -hT
;;
m)
	free -m
;;
u)
	uptime
;;
q)
	exit
;;
*)
	echo "error"
;;
esac

done

例子5

[guan@nfs ~]$ ssh-keygen    //做免密
Generating public/private rsa key pair.
Enter file in which to save the key (/home/guan/.ssh/id_rsa): 
Created directory '/home/guan/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/guan/.ssh/id_rsa.
Your public key has been saved in /home/guan/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1yN8meQaoTZIydRAqdVW3cIr6uwVv9fv/TX3mbAjeQ0 guan@nfs
The key's randomart image is:
+---[RSA 2048]----+
|     .+= ..o .   |
|     oo.+   + .  |
|     o+.  . .o   |
|    .. . o.=.o   |
|      . S.*.B    |
|       ..o B E   |
|       o  o o.ooo|
|        o. o ++.X|
|       ..   +o.+O|
+----[SHA256]-----+
[guan@nfs ~]$ ssh-copy-id 192.168.200.184
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/guan/.ssh/id_rsa.pub"
The authenticity of host '192.168.200.184 (192.168.200.184)' can't be established.
ECDSA key fingerprint is SHA256:ygT6h9ejxNmaemQtyIzVYHEbRko0BaG4PstS2LTavDM.
ECDSA key fingerprint is MD5:88:1f:c4:d2:fe:73:b9:1f:7f:26:cd:2c:ba:ad:5c:b5.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.200.184's password: 
Permission denied, please try again.
root@192.168.200.184's password: 
Permission denied, please try again.
guan@192.168.200.184's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.200.184'"
and check to make sure that only the key(s) you wanted were added.



[root@nfs test]# bash jump.sh  //运行脚本即可跳转服务器
1.web1
2.web2
3.nfs
4.quit
please input a num :1
The authenticity of host 'web1 (192.168.200.184)' can't be established.
ECDSA key fingerprint is SHA256:ygT6h9ejxNmaemQtyIzVYHEbRko0BaG4PstS2LTavDM.
ECDSA key fingerprint is MD5:88:1f:c4:d2:fe:73:b9:1f:7f:26:cd:2c:ba:ad:5c:b5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'web1' (ECDSA) to the list of known hosts.
Last login: Sat Dec 31 15:46:59 2022 from 192.168.200.182
[root@web1 ~]# exit
登出
Connection to web1 closed.
1.web1
2.web2
3.nfs
4.quit
please input a num :3
The authenticity of host 'nfs (192.168.200.182)' can't be established.
ECDSA key fingerprint is SHA256:ygT6h9ejxNmaemQtyIzVYHEbRko0BaG4PstS2LTavDM.
ECDSA key fingerprint is MD5:88:1f:c4:d2:fe:73:b9:1f:7f:26:cd:2c:ba:ad:5c:b5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'nfs,192.168.200.182' (ECDSA) to the list of known hosts.
root@nfs's password: 
Last login: Sat Dec 31 15:42:16 2022
[root@nfs ~]# exit
登出
Connection to nfs closed.
1.web1
2.web2
3.nfs
4.quit
please input a num :4



[root@nfs test]# cat jump.sh
#!/bin/bash
#定义目标主机
web1=192.168.200.184
web2=192.168.200.183
nfs=192.168.200.182

#跳转菜单退出
while :
do

#打印跳转菜单
cat <<EOF
1.web1
2.web2
3.nfs
4.quit
EOF

#读取用户输入
read -p "please input a num :" num

#判断用户的选择
case $num in
1)
ssh root@web1
;;
2)
ssh root@web2
;;
3)
ssh root@nfs
;;
4)
exit
;;
esac

done

[root@nfs ~]# vim .bash_profile
[root@nfs ~]# cat .bash_profile  //将脚本加入开机自启文件会自动帮你执行脚本文
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
/root/test/jump.sh

[root@nfs test]# exit
登出
[guan@nfs ~]$ exit
登出

Connection closed.

Disconnected from remote host(192.168.200.182) at 16:33:36.

Type `help' to learn how to use Xshell prompt.
[C:~]$ ssh root@192.168.200.182


Connecting to 192.168.200.182:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Sat Dec 31 15:59:12 2022 from 192.168.200.182
-bash: /root/test/jump.sh: 权限不够
[root@nfs test]# ls -l
-rw-r--r-- 1 root root 366 12月 31 15:35 jump.sh

[root@nfs test]# chmod +x jump.sh 
[root@nfs test]# ls -l jump.sh 
-rwxr-xr-x 1 root root 366 12月 31 15:35 jump.sh
[C:~]$ ssh root@192.168.200.182


Connecting to 192.168.200.182:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Sat Dec 31 16:40:47 2022 from 192.168.200.1
1.web1
2.web2
3.nfs
4.quit
please input a num :4
[root@nfs ~]# 



原文地址:https://blog.csdn.net/guanguan12319/article/details/128494732

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_35438.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注