标签:生成 ordinary hose ret iss ram direct 传参数 http
特殊变量
$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
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
#!/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
#!/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}
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
-i
:忽略大小写 -n
:显示行数 -r
:搜索目录,不指定的话,目录不搜索 -v
:选择不配的行 -c
:计算符合样式的行数 -w
:全词匹配测试文本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 -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#
$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
$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
$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
$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
$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
$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
$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
内建变量
$0
:当前记录(这个变量中存放着整个行的内容)
$1-$n
:当前记录的第n个字段,字段间由FS分隔
NF
:当前记录中的字段个数,就是有多少列
NR
:已经读出的记录数,就是行号,从1开始,如果有多个文件话,这个值也是不断累加中。
FNR
:当前记录数,与NR不同的是,这个值会是各个文件自己的行号
RS
:输入字段分隔符 默认是空格或Tab
FS
:输入的记录分隔符, 默认为换行符
ORS
:输出字段分隔符, 默认也是空格
OFS
:输出的记录分隔符,默认为换行符
FILENAME
:当前输入文件的名字
AWK 脚本
学生成绩表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\n" printf "---------------------------------------------\n"}
#运行中
{ math+=$3 english+=$4 computer+=$5 printf "%-6s %-6s %4d %8d %8d %8d\n", $1, $2, $3,$4,$5, $3+$4+$5}
#运行后END
{ printf "---------------------------------------------\n" printf " TOTAL:%10d %8d %8d \n", math, english, computer printf "AVERAGE:%10.2f %8.2f %8.2f\n", math/NR, english/NR, computer/NR}
一般脚本
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
find . -name "*.c" | xargs grep "a_string" -l| xargs sed -i "s/a_string/b_string/g"
变量通过" "引号引起来
#!/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
标签:生成 ordinary hose ret iss ram direct 传参数 http
原文地址:https://www.cnblogs.com/standardzero/p/12552996.html