【 shell 编程 】第2篇 判断

news2024/9/28 17:36:04

判断


文章目录

  • 判断
  • 一、条件测试
  • 二、流程控制
    • 1.单分支结构
    • 2.双分支结构
    • 3.多分支结构
    • 4.嵌套结构
  • 三、匹配模式


在这里插入图片描述


一、条件测试

格式:

格式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)
&& 逻辑的 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 &> /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 &> /dev/null

if [ $? -ne 0 ];then
	useradd $name
	echo "123456 | passwd --stdin $name &> /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 &> /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 &> /dev/null
	then
	useradd $name
	echo "123456 | passwd --stdin $name &> /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 &> /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 &> /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
需求:编写一个脚本,由用户输入用户名,判断该用户的uid及gid,如果相同,则显示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 ~]# 



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/130599.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

电气器件系列三十五:开关电源选型实例

开关模式电源&#xff08;Switch Mode Power Supply&#xff0c;简称SMPS&#xff09;&#xff0c;又称交换式电源、开关变换器&#xff0c;是一种高频化电能转换装置&#xff0c;是电源供应器的一种。其功能是将一个位准的电压&#xff0c;透过不同形式的架构转换为用户端所需…

c++11 标准模板(STL)(std::deque)(三)

定义于头文件 <deque> std::deque 赋值给容器 std::deque<T,Allocator>::operator deque& operator( const deque& other ); (1) deque& operator( deque&& other ); (2)(C11 起) (C17 前) deque& operator( deque&& other ) no…

Docker的基本使用

文章目录1.初识Docker1.1 为什么会有docker出现&#xff1f;1.2 docker的理念1.3 Docker和虚拟机的区别1.4 Docker的基本组成(docker的三要素)1.4.1 镜像1.4.2 容器1.4.3 仓库1.5 Docker的架构2.Docker的安装3.常用命令3.1 帮助启动类命令3.2 镜像命令3.3 容器命令1.初识Docker…

使用Lua Script实现不同的限流算法

文章目录Redis中执行Lua ScriptRedis中Debug Lua Script固定窗口代码关键部分解释验证滑动窗口令牌桶漏桶参考资料Redis中执行Lua Script redis-cli --eval /tmp/script.lua mykey somekey , arg1 arg2特别注意&#xff1a;key和arg之间是空格逗号空格&#xff0c;否则脚本调用…

API网关在API安全性中的作用

从单一应用程序切换到微服务时&#xff0c;客户端的行为不能与客户端具有该应用程序的一个入口点的行为相同。简单来说就是微服务上的某一部分功能与单独实现该应用程序时存在不同。 目前在使用微服务时&#xff0c;客户端必须处理微服务体系结构带来的所有复杂性&#xff0c;…

一个月裂变50多万人?商城引流模式玩法——分享购

随着经济进入转型的关键期&#xff0c;零售行业的营销模式正在发生转变&#xff0c;以消费者为中心驱动营销数字化成为当下企业的共识。 新零售时代数字化的核心是数据能力的全面升级&#xff0c;企业通过数字化协同能力整合线上线下全域消费场景&#xff0c;赋能消费者深度洞…

JDK16及其以后的版本Maven打包成可运行的程序的方法

Java应用的分发一直是一个比较麻烦的问题。这是因为Java应用的运行需要虚拟机的支持&#xff0c;仅有Java应用打包的JAR文件是不够的&#xff0c;目标机器还需要安装版本匹配的JDK或JRE。随着云原生和容器化技术的流行&#xff0c;Java应用可以选择以容器镜像的形式来打包和分发…

以太网,拥塞控制与 AQM

拥塞的表现是冲突&#xff0c;大量消费者挤兑有限资源&#xff0c;即拥塞&#xff0c;在一个绝对没有冲突的系统里&#xff0c;自然就没有拥塞。而冲突的来源有两个&#xff0c;一个是无序&#xff0c;一个是贪婪。 仅引入秩序不能解决冲突&#xff0c;但可减少冲突损耗&#…

JavaScript高级 浏览器的渲染原理

浏览器的渲染原理1. 网页的解析过程2. 浏览器内核3. 浏览器渲染过程1. HTML解析2. 生成CSS规则3. 构建Render Tree4. 布局&#xff08;layout&#xff09;5. 绘制&#xff08;Paint&#xff09;4. 回流和重绘解析1. 回流2. 重绘3. web 性能优化4. 合成和性能优化5. 浏览器遇到 …

【VulnHub靶场】——CHRONOS: 1

作者名&#xff1a;Demo不是emo 主页面链接&#xff1a;主页传送门创作初心&#xff1a;舞台再大&#xff0c;你不上台&#xff0c;永远是观众&#xff0c;没人会关心你努不努力&#xff0c;摔的痛不痛&#xff0c;他们只会看你最后站在什么位置&#xff0c;然后羡慕或鄙夷座右…

PaddlePaddle/PaddleX本地离线安装(分别以C++和Python为例)

目录一. 本地离线安装简介1.1 为什么需要源码编译1.2 源码编译的产物1.3 源码编译的原理二. 本地离线安装准备2.1 编译准备(C/Python通用)2.2 本地编译推荐配置(C)2.3 本地编译推荐配置(Python)三. Windows本地离线安装(C源码编译)3.2 编译工具安装3.3 打开Visual studio 终端3…

torch.cat函数

#dim0,上下拼接 #dim1&#xff0c;左右拼接 import torch # x1 x1 torch.tensor([[1,2,3],[4,5,6]],dtypetorch.int)# x2 x2 torch.tensor([[7,8,9],[10,11,12]],dtypetorch.int)#inputs为&#xff12;个形状为[2 , 3]的矩阵 inputs [x1, x2] print(inputs)#dim0,上下拼接…

如何用Python字符串进行切片操作?

嗨害大家好鸭&#xff01;我是小熊猫~ 我们基本上都知道Python的序列对象 都是可以用索引号来引用的元素的&#xff0c; 索引号可以是正数由0开始从左向右&#xff0c; 也可以是负数由-1开始从右向左。 在Python中对于具有序列结构的数据来说都可以使用切片操作&#xff0…

【C++升级之路】第五篇:C/C++内存管理(new和delete的实现原理)

&#x1f31f;hello&#xff0c;各位读者大大们你们好呀&#x1f31f; &#x1f36d;&#x1f36d;系列专栏&#xff1a;【C学习与应用】 ✒️✒️本篇内容&#xff1a;C/C内存分布&#xff0c;C/C动态内存管理方法&#xff0c;C动态内存管理方法底层函数operator new 和operat…

浅谈树状数组

学习树状数组必不可少学习树状数组的精髓&#xff0c;lowbit()运算 在计算机中存储一个正数是以二进制的形式&#xff0c;而存储一个负数则是以二进制补码的形式&#xff0c;简单说就是二进制取反1&#xff0c;lowbit运算就是提取出最后一个1以后的位置&#xff0c;比如10100进…

【C语言】结构体、共用体、位域

结构体 1、 结构体的声明方法 struct struct_name {data_type member1;data_type member2;.. };这是其中一种声明方式~ 2、定义一个结构体变量 struct struct_name variable&#xff1b;3、访问成员变量 . 运算 一个结构体变量访问其成员时&#xff0c;使用的是 . 运算 下面…

BM35 判断是不是完全二叉树

题目 给定一个二叉树&#xff0c;确定他是否是一个完全二叉树。 完全二叉树的定义&#xff1a;若二叉树的深度为 h&#xff0c;除第 h 层外&#xff0c;其它各层的结点数都达到最大个数&#xff0c;第 h 层所有的叶子结点都连续集中在最左边&#xff0c;这就是完全二叉树。&a…

深入理解ConcurrentHashMap1.7源码

1. 概述 HashMap在我们的日常生活中使用很多&#xff0c;但是它不是线程安全的。我们可以使用HashTable来代替&#xff0c;主要实现方式是在方法中加入synchronized&#xff0c;所以效率也比较低。因此&#xff0c;对于键值对&#xff0c;我们可以尝试使用ConcurrentHashMap来…

实验室规划设计方案SICOLAB

一、实验室规划设计 喜格提供实验室布局方案 根据实验室性质、实验室定位、实验室功能、实验类型、实验工艺流程以及国家相关标准合理的规划布局。 喜格提供仪器摆放布局方案 根据该实验流程来确定仪器的种类、数量、规格型号、外形尺寸、电压功率等参数以及摆放位置以及提…

【Linux】tee、tail、killall、|、||、、命令学习

|、||、&、&&辨析 竖线‘|’在linux中是管道符的意思&#xff0c;将‘|’前面命令的输出作为’|后面的输入&#xff1b; 双竖线‘||’&#xff0c;用双竖线‘||’分割的多条命令&#xff0c;执行的时候遵循如下规则&#xff1a;如果前一条命令为真&#xff0c;则…