test
-f 是判断文件是否存在
-d 判断目录是否存在
-n 判断字符串的长度是否不为0 nonezero
-n STRING
the length of STRING is nonzero
-z 判断字符的长度是否为0 zero
-z STRING
the length of STRING is zero
-e 判断是否存在 exist
使用test命令后,通过$?命令查看命令是否执行成功,返回0就成功,非0不成功
[root@lier lianxi]# test -f xiejiaxin.txt
[root@lier lianxi]# echo $?
0
[root@lier lianxi]# test -f saflkj
[root@lier lianxi]# echo $?
1
[root@lier lianxi]# test -d lier
[root@lier lianxi]# echo $?
0
[ ]
判断文件、文件夹是否存在
同test命令作用一样
[root@lier lianxi]# [ -f lwq.txt ]
[root@lier lianxi]# echo $?
0
[root@lier lianxi]# [ -d lier ]
[root@lier lianxi]# echo $?
0
[[ ]]
字符串的比较,[[]]和[]都可以进行字符串的比较
[[ ]] -->推荐,因为字符串里有空格也可以比较
[ ]
双中括号比较:
[root@master ~]# sg="wenke"
[root@master ~]# [[ $sg == "wenkeke" ]]
[root@master ~]# echo $?
1
[root@master ~]#
单中括号比较:
[root@master ~]# [ $sg == "wenke" ]
[root@master ~]# echo $?
0
[root@master ~]#
[root@master ~]# sg="wenke wenke"
[root@master ~]# [ $sg == "wenke wenke" ]
-bash: [: 参数太多
[root@master ~]#
[root@master ~]# [[ $sg == "wenke wenke" ]]
[root@master ~]# echo $?
0
[root@master ~]#
当字符串中有空格时,单中括号就会认为有多个变量
-f FILE # 文件
FILE exists and is a regular file
-d FILE # 文件夹
FILE exists and is a directory
-e FILE
FILE exists
-n STRING
the length of STRING is nonzero
-z STRING
the length of STRING is zero
[root@lier ~]# mn='afdafdfa'
[root@lier ~]# [ -n $mn ] # 判断长度是否不为0
[root@lier ~]# echo $?
0
[root@lier ~]# [ -z $mn ] # 判断长度是否为0
[root@lier ~]# echo $?
1
[root@lier ~]# [ -e $mn ] 判断文件是否存在
[root@lier ~]# echo $?
1
[root@lier ~]# [ -e /lianxi ]
[root@lier ~]# echo $?
0
查看字符串长度
[root@lier 517]# echo $sg
wenke
[root@lier 517]# echo ${#sg}
5
[root@lier ~]# mn='afdafdfa'
[root@lier ~]# echo ${#mn}
8
练习
编写脚本判断用户输入的用户名和密码是否正确
密码的长度必须要求8位
比较用户名,还要比较密码
位置变量
[root@master yum.repos.d]# bash a.sh feng 123456 sanchuang
脚本名 $1 $2 $3
1.脚本名 user_passwd.sh
[root@master 512]# vim user_passwd.sh
#!/bin/bash
#username admin passwd: 123456sc
#用户名的判断
if [[ $1 == "admin" ]];then
echo "恭喜你,成功登录系统"
else
echo "用户名不正确,请重新登录"
fi
======
[root@master 512]# bash user_passwd.sh feng
用户名不正确,请重新登录
[root@master 512]# bash user_passwd.sh admin
恭喜你,成功登录系统
[root@master 512]#
============
[root@master 512]# cat user_passwd.sh
#!/bin/bash
#username admin passwd: 123456sc
#用户名的判断
if [[ $1 == "admin" ]];then
echo "恭喜你,用户存在"
#num_pwd=$2
if (( ${#2} >=8 ));then
echo "密码长度大于8位,接着验证密码是否正确"
else
echo "密码长度少于8位,请重新输入"
fi
else
echo "用户名不正确,请重新登录"
fi
[root@master 512]#
[root@master 512]# bash user_passwd.sh admin 123456
恭喜你,用户存在
密码长度少于8位,请重新输入
[root@master 512]# bash user_passwd.sh admin 123456789
恭喜你,用户存在
密码长度大于8位,接着验证密码是否正确
[root@master 512]#
===
[root@master 512]# cat user_passwd.sh
#!/bin/bash
#username admin passwd: 123456sc
#用户名的判断
if [[ $1 == "admin" ]];then
echo "恭喜你,用户存在"
#num_pwd=$2
if (( ${#2} >=8 ));then
echo "密码长度大于8位,接着验证密码是否正确"
if [[ $2 == "123456sc" ]];then
echo "恭喜你!用户名和密码正确,登录系统成功"
else
echo "sorry,你的密码不正确,请重新登录"
fi
else
echo "密码长度少于8位,请重新输入"
fi
else
echo "用户名不正确,请重新登录"
fi
[root@master 512]# bash user_passwd.sh feng 123456
用户名不正确,请重新登录
[root@master 512]# bash user_passwd.sh admin 123456
恭喜你,用户存在
密码长度少于8位,请重新输入
[root@master 512]# bash user_passwd.sh admin 1234568987
恭喜你,用户存在
密码长度大于8位,接着验证密码是否正确
sorry,你的密码不正确,请重新登录
[root@master 512]# bash user_passwd.sh admin 123456sc
恭喜你,用户存在
密码长度大于8位,接着验证密码是否正确
恭喜你!用户名和密码正确,登录系统成功
[root@master 512]#
脚本中对密码进行加密:
[root@lier ~]# echo "sc123456"|md5sum
de9492d310d18392cc0c79e8e759b36a -
[root@lier ~]# echo "sc123456"|base64
c2MxMjM0NTYK
最终代码:
===
[root@master 512]# cat user_passwd.sh
#!/bin/bash
#username admin passwd: 123456sc
#用户名的判断
if [[ $1 == "admin" ]];then
echo "恭喜你,用户存在"
#num_pwd=$2
if (( ${#2} >=8 ));then
echo "密码长度大于8位,接着验证密码是否正确"
user_pwd=$(echo $2|base64)
if [[ $user_pwd == "MTIzNDU2c2MK" ]];then
echo "恭喜你!用户名和密码正确,登录系统成功"
else
echo "sorry,你的密码不正确,请重新登录"
fi
else
echo "密码长度少于8位,请重新输入"
fi
else
echo "用户名不正确,请重新登录"
fi
[root@master 512]#
linux中设置python镜像源
1、创建文件夹(如果有就直接执行下一步)
mkdir ~/.pip
2、创建文件
vim ~/.pip/pip.conf
3、编辑文件pip.conf
[root@master yum.repos.d]# cat ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
[root@master yum.repos.d]#
4、测试是否生效
[root@master yum.repos.d]# pip3 install pymysql 安装pymysql库
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pymysql
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4f/52/a115fe175028b058df353c5a3d5290b71514a83f67078a6482cff24d6137/PyMySQL-1.0.2-py3-none-any.whl (43kB)
100% |████████████████████████████████| 51kB 3.6MB/s
Installing collected packages: pymysql
Successfully installed pymysql-1.0.2
[root@master yum.repos.d]#