1. shell 简明教程

Posted standardzero

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1. shell 简明教程相关的知识,希望对你有一定的参考价值。

TOC

1.变量运算

特殊变量

$0:当前脚本变量名字

$n:传入脚本或函数的第几个参数

$#:传输脚本或函数的参数个数

$*:传给脚本或函数的所有参数

$@:传给脚本或函数的所有参数

$?:上一个命令的返回值

$$: 当前shell 进程ID

测试脚本

#!/bin/bash


a=20
b=10


val=$(expr ${a} + ${b})
echo ${val}


val=$(expr ${a} - ${b})
echo ${val}


val=$(expr ${a} * ${b})
echo ${val}


val=$(expr ${a} / ${b})
echo ${val}


val=$(expr ${a} % ${b})
echo ${val}


if [ ${a} == ${b} ];then
    echo "a == b"
fi


if [ ${a} != ${b} ];then
    echo "a != b"
fi

测试脚本

#!/bin/bash


name1="mark"
name2="mark"


age1=22
age2=22


#字符串比较使用 = or !=
if [ ${name1} = ${name2} -a ${age1} -eq ${age2} ];then
    echo "a == b"
else
    echo "a != b"
fi

测试脚本

#!/bin/bash


a="abc"
b="123"


if [ $a = $b ];
then
    echo "$a = $b : a is equal to b"
else
    echo "$a = $b: a is not equal to b"
fi


if [ $a != $b ];
then
    echo "$a != $b : a is not equal to b"
else
    echo "$a != $b: a is equal to b"
fi


if [ -z $a ];
then
    echo "-z $a : string length is zero"
else
    echo "-z $a : string length is not zero"
fi


if [ -n $a ];
then
    echo "-n $a : string length is not zero"
else
    echo "-n $a : string length is zero"
fi


if [ $a ];
then
    echo "$a : string is not empty"
else
    echo "$a : string is empty"
fi

测试脚本

#!/bin/bash


file="test.txt"


if [ -r $file ]
then
    echo "File has read access"
else
    echo "File does not have read access"
fi
if [ -w $file ]
then
    echo "File has write permission"
else
    echo "File does not have write permission"
fi
if [ -x $file ]
then
    echo "File has execute permission"
else
    echo "File does not have execute permission"
fi
if [ -f $file ]
then
    echo "File is an ordinary file"
else
    echo "This is sepcial file"
fi
if [ -d $file ]
then
    echo "File is a directory"
else
    echo "This is not a directory"
fi
if [ -s $file ]
then
    echo "File size is zero"
else
    echo "File size is not zero"
fi
if [ -e $file ]
then
    echo "File exists"
else
    echo "File does not exist"
fi

2.条件分支语句

if elif else


compare()
{
    if [ $1 -eq $2 ];then
        echo $1 "==" $2
    elif [ $1 -lt $2 ];then
        echo $1 "<" $2
    else
        echo $1 ">" $2
    fi


}


compare 1 1
compare 1 2
compare 2 1

case esac


chose()
{
    case $1 in
        1)
            echo "step 1"
            ;;
        2)
            echo "step 2"
            ;;
        *)
            echo "error"
            ;;
    esac
}


chose 1
chose 2
chose 4

3.循环语句

#!/bin/bash


for_loop()
{
    for val in $*
    do
        echo ${val}
    done
}



for_loop this is my program


for_num()
{
    for((i=1; i < 5; i++))
    do
        echo ${i}
    done
}


for_num



while_loop()
{
    i=0
    while [[ $i < 5 ]]
    do
        echo "step " ${i}
        ((i=i+2))
    done


}


while_loop

4.函数

#!/bin/bash


Hello()
{
    echo "Hello World"
}


Hello


Print()
{
    echo "Print: " ${1} ${2}
}


#函数传参数
Print Hello World


#函数返回值
Sum()
{
    val=$(expr $1 + $2)


    return ${val}
}


Sum 1 2
#$? 返回值
val=$?


echo "val = " ${val}

5.read 使用

  • 遍历文件

student.info

mark:22
lisa:22
dark:23

测试脚本

#!/bin/bash


#遍历文件
while read line
do
  echo ${line}
done < student.info


#遍历文件
cat student.info | while read line
do
    echo ${line}
done


#获取变量, IFS表示分隔符
while IFS=: read name age
do
    echo ${name} ${age}
done < student.info

6.sort 使用

7.grep 使用

  • 常用参数
    -i:忽略大小写
    -n:显示行数
    -r:搜索目录,不指定的话,目录不搜索
    -v:选择不配的行
    -c:计算符合样式的行数
    -w:全词匹配

8.sed 使用

测试文本pets

This is my cat
my cat‘s name is betty
This is my dog
my dog‘s name is frank
This is my fish
my fish‘s name is george
This is my goat
my goat‘s name is adam
  • 替换my为your
$sed -i ‘s/my/your/g‘ petsThis is your cat
your cat‘s name is betty
This is your dog
your dog‘s name is frank
This is your fish
your fish‘s name is george
This is your goat
your goat‘s name is adam
  • 行首添加#
$sed ‘s/^/#/g‘ pets#This is my cat
#my cat‘s name is betty
#This is my dog
#my dog‘s name is frank
#This is my fish
#my fish‘s name is george
#This is my goat
#my goat‘s name is adam

  • 行尾添加#
$sed ‘s/$/#/g‘ petsThis is my cat#
my cat‘s name is betty#
This is my dog#
my dog‘s name is frank#
This is my fish#
my fish‘s name is george#
This is my goat#
my goat‘s name is adam#

  • 1到3行的my替换为your
$sed ‘1,3s/my/your/g‘ petsThis is your cat
your cat‘s name is betty
This is your dog
my dog‘s name is frank
This is my fish
my fish‘s name is george
This is my goat
my goat‘s name is adam

  • 只替换每一行的第一个i
$sed -i ‘s/i/I/1‘ petsThIs is my cat
my cat‘s name Is betty
ThIs is my dog
my dog‘s name Is frank
ThIs is my fish
my fIsh‘s name is george
ThIs is my goat
my goat‘s name Is adam

  • 只替换每一行的第二个以后的i
$sed ‘s/i/I/2g‘ petsThis Is my cat
my cat‘s name is betty
This Is my dog
my dog‘s name is frank
This Is my fIsh
my fish‘s name Is george
This Is my goat
my goat‘s name is adam

  • 一次使用多次匹配模式
$sed ‘1,3s/my/your/g ; 4,$s/my/her/g‘ petsThis is your cat
your cat‘s name is betty
This is your dog
her dog‘s name is frank
This is her fish
her fish‘s name is george
This is her goat
her goat‘s name is adam

  • i 命令插入一行
$sed ‘1 i This is my monkey‘ petsThis is my monkey
This is my cat
my cat‘s name is betty
This is my dog
my dog‘s name is frank
This is my fish
my fish‘s name is george
This is my goat
my goat‘s name is adam

  • a 命令插入一行
$sed ‘1 a This is my monkey‘ petsThis is my cat
This is my monkey
my cat‘s name is betty
This is my dog
my dog‘s name is frank
This is my fish
my fish‘s name is george
This is my goat
my goat‘s name is adam

  • c 替换一行
$sed ‘1 c This is my monkey‘ petsThis is my monkey
my cat‘s name is betty
This is my dog
my dog‘s name is frank
This is my fish
my fish‘s name is george
This is my goat
my goat‘s name is adam

  • d 删除一行
$sed ‘2d‘ petsThis is my cat
This is my dog
my dog‘s name is frank
This is my fish
my fish‘s name is george
This is my goat
my goat‘s name is adam
  • 匹配删除行
$sed ‘/This/d‘ petsmy cat‘s name is betty
my dog‘s name is frank
my fish‘s name is george
my goat‘s name is adam

9.awk 使用

内建变量
$0:当前记录(这个变量中存放着整个行的内容)
$1-$n:当前记录的第n个字段,字段间由FS分隔
NF:当前记录中的字段个数,就是有多少列
NR:已经读出的记录数,就是行号,从1开始,如果有多个文件话,这个值也是不断累加中。
FNR:当前记录数,与NR不同的是,这个值会是各个文件自己的行号
RS:输入字段分隔符 默认是空格或Tab
FS:输入的记录分隔符, 默认为换行符
ORS:输出字段分隔符, 默认也是空格
OFS:输出的记录分隔符,默认为换行符
FILENAME:当前输入文件的名字

AWK 脚本

  • BIGIN{这里面放的是执行前的语句}
  • END{这里面放的是执行后的语句}
  • {这里面放的是处理每一行需要执行的语句}

学生成绩表score

Marry   2143 78 84 77
Jack    2321 66 78 45
Tom     2122 48 77 71
Mike    2537 87 97 95
Bob     2415 40 57 62

awk 脚本

#!/bin/awk -f
#运行前
BEGIN {    math = 0    english = 0    computer = 0     printf "NAME    NO.   MATH  ENGLISH  COMPUTER   TOTAL
"    printf "---------------------------------------------
"}
#运行中
{    math+=$3    english+=$4    computer+=$5    printf "%-6s %-6s %4d %8d %8d %8d
", $1, $2, $3,$4,$5, $3+$4+$5}
#运行后END
{    printf "---------------------------------------------
"    printf "  TOTAL:%10d %8d %8d 
", math, english, computer    printf "AVERAGE:%10.2f %8.2f %8.2f
", math/NR, english/NR, computer/NR}

10.获取当前脚本所在的绝对路

一般脚本

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

链接文件脚本

SOURCE="${BASH_SOURCE[0]}"while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"  SOURCE="$(readlink "$SOURCE")"  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was locateddoneDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

参考:http://blog.csdn.net/weijinhua_123/article/details/53152411

11. 批量替换文件里面的内容

find . -name "*.c" | xargs grep "a_string" -l| xargs sed -i "s/a_string/b_string/g"

12. 如何判断一个变量是否被赋值

变量通过" "引号引起来

#!/bin/sh
para1=
if [ ! -n "$para1" ]; then
  echo "IS NULL"
else
  echo "NOT NULL"
fi

直接通过变量判断

#!/bin/sh
para1=
if [ ! $para1 ]; then
  echo "IS NULL"
else
  echo "NOT NULL"
fi

使用test判断

#!/bin/sh
dmin=
if test -z "$dmin"
then
  echo "dmin is not set!"
else 
  echo "dmin is set !"
fi

使用""判断

#!/bin/sh
dmin=
if [ "$dmin" = "" ]
then
  echo "dmin is not set!"
else 
  echo "dmin is set !"
fi



























以上是关于1. shell 简明教程的主要内容,如果未能解决你的问题,请参考以下文章

markdown 打字稿...编码说明,提示,作弊,指南,代码片段和教程文章

vue2.0 代码功能片段

shell 脚本 片段

nodejs常用代码片段

Shell编程入门

用于确保在任何给定时间仅运行一个 shell 脚本的 shell 片段 [重复]