linux shell知识汇总

方法一:利用while do循环,举例,while true;do ls;sleep 1;done 

方法二:利用for循环,举例1,for((a=1;a<=10;a++));do echo "haha" ;done

                                     举例2,for i in {host1,host2,host3};do ping -c 4 $i;done

2、shell中a++的写法

a=1  

a=$(($a+1))  

a=$[$a+1]  

a=`expr $a + 1`  

let a++  

let a+=1  

3、g e t o p t s

g e t o p t s可以编写脚本,使控制多个命令行参数更加容易。g e t o p t s用于形成命令行处理标 
准形式

实例:

#!/bin/bash
 
while getopts "a:b:" opt; do
  case $opt in
    a)
      echo "$OPTARG" 
      ;;
    b)
      echo "$OPTARG"
      ;;
    \?)
      echo "Invalid option: -$OPTARG" 
      ;;
  esac

done
执行结果:

[root@localhost ~]# sh getopts.sh -a hello -b xixi
hello
xixi
 

4、${ }

假设我们定义了一个变量为:
file=/dir1/dir2/dir3/my.file.txt

可以用${ }分别替换得到不同的值:

${file#*/}:删掉第一个/ 及其左边的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}:删掉最后一个/  及其左边的字符串:my.file.txt
${file#*.}:删掉第一个.  及其左边的字符串:file.txt
${file##*.}:删掉最后一个.  及其左边的字符串:txt
${file%/*}:删掉最后一个 /  及其右边的字符串:/dir1/dir2/dir3
${file%%/*}:删掉第一个/  及其右边的字符串:(空值)
${file%.*}:删掉最后一个 .  及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:删掉第一个 .   及其右边的字符串:/dir1/dir2/dir3/my

5、basename

basename 是去除目录后剩下的名字

6、dos2unix

是将Windows格式文件转换为Unix、Linux格式的实用命令。Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2unix命令其实就是将文件中的\r\n 转换为\n。

7、while read line

cat 1.txt| while read line                 
      do  echo "haha"
      done

 while read line                 
      do  echo "haha"
      done < 1.txt


8、tr命令

tr命令不能从文件中读取数据,只能从标准输出中获取数据,结果写到输出设备。

tr命令可以批量转换或删除指定的字符。

语法:

tr [选项]  字符集1 字符集2

选项:

-d:删除指定的字符集1的东西

-s:压缩字符集1中字符。一排重复的字符会压缩成一个

-t:字符集2 替换 字符集1,不加选项同结果。

-c:保留字符集1的字符,其他的字符用字符集2替换。

字符集:

[a-z][A-Z][0-9]:很好理解的范围。

[xx*n]:表示xx出现n次

和所有元字符

实例:

去字符的重复 -s。s选项可以压缩文本中连续出现的字符只保留一个。

[root@ams d6]# echo "hhhhi,woooorllld" > 2.txt

[root@ams d6]# tr -s [a-z]  < 2.txt

hi,world

去除空行

[root@ams d6]# echo -e "aa\n\n\n\n\naa" > 2.txt  

[root@ams d6]# tr -s "\n" < 2.txt  

aa

aa

大小写转换 -t。用第二个字符集 替换 第一个字符集。

[root@ams d6]# ls | tr 'a-z' 'A-Z'

1.TXT

2.TXT

删除指定字符 -d。单个字符直接引用起来就可以,多个字符需要用中括号括起来。

[root@ams d6]# date | tr -d "[0-9] [:]"

WedSepCST

9、shell 中的$0 $1 $* $@ $# $$ $? $() $(())

$0: 脚本本身文件名称

$1: 命令行第一个参数,

$2为第二个,以此类推

$*: 所有参数列表$@: 所有参数列表

$#: 参数个数

$$: 脚本运行时的PID

$?: 脚本退出码,以及获取函数的返回值,请看以下实例

#!/bin/bash

function add(){

return `expr $1 + $2`

}

add 23 50 #调用函数

echo $? #获取函数返回值

∗与@的区别

当命令行为test.sh 1 2 3"$*“表示"1 2 3"$@“表示"1” “2” “3

二者没有被引号括起来时是一样的都为"1 2 3",只有当被引号括起来后才表现出差异

$()  等同于: ··(反引号):运行一段命令

$(()) 进行数字运算 

# a=3;b=2;c=5

# echo $((a+b*c))

10、shell-if表达式(-f,-d,-s,-r,-w,-x,-eq,-ne,-ge,-gt,-le,-lt )

原文链接:shell-if表达式(-f,-d,-s,-r,-w,-x,-eq,-ne,-ge,-gt,-le,-lt )_打卤的博客-CSDN博客

文件表达式
if [ -f file ] 如果文件存在
if [ -d … ] 如果目录存在
if [ -s file ] 如果文件存在且非空
if [ -r file ] 如果文件存在且可读
if [ -w file ] 如果文件存在且可写
if [ -x file ] 如果文件存在且可执行

整数变量表达式
if [ int1 -eq int2 ] 如果int1等于int2
if [ int1 -ne int2 ] 如果不等于
if [ int1 -ge int2 ] 如果>=
if [ int1 -gt int2 ] 如果>
if [ int1 -le int2 ] 如果<=
if [ int1 -lt int2 ] 如果<

字符串变量表达式
If [ $a = $b ] 如果string1等于string2
字符串允许使用赋值号做等号
if [ $string1 != $string2 ] 如果string1不等于string2
if [ -n $string ] 如果string 非空(非0),返回0(true)
if [ -z $string ] 如果string 为空
if [ $sting ] 如果string 非空,返回0 (和-n类似)

s​h​e​l​l​中​条​件​判​断​i​f​中​的​-​z​到​-​d​的​意​思
[ -a FILE ] 如果 FILE 存在则为真。
[ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真。

[ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真。

[ -d FILE ] 如果 FILE 存在且是一个目录则为真。

[ -e FILE ] 如果 FILE 存在则为真。
[ -f FILE ] 如果 FILE 存在且是一个普通文件则为真。

[ -g FILE ] 如果 FILE 存在且已经设置了SGID则为真。
[ -h FILE ] 如果 FILE 存在且是一个符号连接则为真。

[ -k FILE ] 如果 FILE 存在且已经设置了粘制位则为真。 [

-p FILE ] 如果 FILE 存在且是一个名字管道(F如果O)则为真。

[ -r FILE ] 如果 FILE 存在且是可读的则为真。

[ -s FILE ] 如果 FILE 存在且大小不为0则为真。
[ -t FD ] 如果文件描述符 FD 打开且指向一个终端则为真。

[ -u FILE ] 如果 FILE 存在且设置了SUID (set user ID)则为真。

[ -w FILE ] 如果 FILE 如果 FILE 存在且是可写的则为真。

[ -x FILE ] 如果 FILE 存在且是可执行的则为真。

[ -O FILE ] 如果 FILE 存在且属有效用户ID则为真。

[ -G FILE ] 如果 FILE 存在且属有效用户组则为真。 [ -L FILE ] 如果 FILE 存在且是一个符号连接则为真。
[ -N FILE ] 如果 FILE 存在 and has been mod如果ied since it was last read则为真。
[ -S FILE ] 如果 FILE 存在且是一个套接字则为真。
[ FILE1 -nt FILE2 ] 如果 FILE1 has been changed more recently than FILE2,or 如果 FILE1 exists and FILE2 does not则为真。
[ FILE1 -ot FILE2 ] 如果 FILE1 比 FILE2 要老, 或者 FILE2 存在且 FILE1 不存在则为真。
[ FILE1 -ef FILE2 ] 如果 FILE1 和 FILE2 指向相同的设备和节点号则为真。

[ -o OPTIONNAME ] 如果 shell选项 “OPTIONNAME” 开启则为真。

[ -z STRING ] “STRING” 的长度为零则为真。
 

11、判断字符串为空

if [ -z $str ];then
    echo "是空字符串"
fi

if [ "$str" ];then
   echo "非空"
else
    echo "空"
fi

if [ test -z "$str" ];then
   echo "空"
else
    echo "非空"
fi

if [ !-n  "$str" ];then
   echo "空"
else
    echo "非空"
fi

12、shell获取当前路径

CURRENT_DIR="$(cd "$(dirname "$0")"; pwd)"

13、内部字段分隔符的用法

内部字段分隔符(Internal Field Separator,IFS)是shell脚本中的一个特殊变量,在处理文本数据很有用。把单个数据流划分成不同的数据元素的定界符,
内部字段分隔符就是用于特定用途的定界符。IFS是存储定界符的环境变量,是shell环境中的默认定界符字符串。
默认值为空白字符(换行符\n,制表符\t,空格)

#!/bin/bash 
OLD_IFS="${IFS}"   ###定义一个变量为默认的IFS,将当前值保存
IFS=,
data=11,22,33,44
for line in $data;do
    echo $line
done
IFS="${OLD_IFS}"    ###还原IFS为默认值

运行脚本结果:
11
22
33
44

14、获取上个月第一天和最后一天

 lastday:date -d "-$(date +%d) days" +%Y%m%d

firstday:date -d "-1 month -$(($(date +%d)-1)) days" +%Y%m%d

15、sshpass用法

sshpass:用于非交互的ssh 密码验证
使用 -p 参数指定明文密码,然后直接登录远程服务器。 它支持密码从命令行,文件,环境变量中读取
1、从命令行方式传递密码
sshpass -p user_password ssh user_name@192.168.1.2  【登录远程机器】
sshpass -p user_password scp -P22 root@192.168.1.2:/home/test  ./ 【远程机器/home/test 复制到本机当前目录】
还可以加参数 -q 【去掉进度显示】

2、从文件读取密码
echo "user_password" > user.passwd
sshpass -f user.passwd ssh user_name@192.168.1.2

3、从环境变量获取密码
export SSHPASS="user_password"
sshpass -e ssh user_name@192.168.1.2 

4、sshpass -p user_password ssh  -o StrictHostKeyChecking=no  user_name@192.168.1.2 
【-o StrictHostKeyChecking=no 表示远程连接时不提示是否输入yes/no】

5、使用sshpass远程免密,在远程主机上执行shell命令,如下远程执行命令:touch /opt/file.txt
sshpass -p user_password ssh  -o StrictHostKeyChecking=no  user_name@192.168.1.2  touch /opt/file.txt
[注:shell命令要和sshpass命令写在一行]

复制自:https://www.jianshu.com/p/a2aaa02f57dd 觉得他总结的很好,复制到自己博客,方便自己下次查找。
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值