脚本以#!/bin/bash
开头(指定bash解析器)
# 创建脚本helloworld.sh:调用时打印hello world
[azula@localhost datas]$ cat helloworld.sh
#!/bin/bash
echo "hello world"# 执行脚本方式1
[azula@localhost datas]$ bash helloworld.sh
hello world# 执行脚本方式2
[azula@localhost datas]$ ./helloworld.sh
-bash: ./helloworld.sh: 权限不够
#!/bin/bash
cd /home/azula/
touch biaobai.txt
echo "I LOVE YOU" >> biaobai.txt
$HOME $PWD $SHELL $USER
# $HOME:当前用户的HOME
[azula@localhost datas]$ echo $HOME
/home/azula
# $PWD:当前路径
[azula@localhost datas]$ echo $PWD
/home/azula/datas
# $SHELL:bash位置
[azula@localhost datas]$ echo $SHELL
/bin/bash
# $USER:当前用户
[azula@localhost datas]$ echo $USER
azula
[azula@localhost datas]$ A=1
[azula@localhost datas]$ echo $A
1
[azula@localhost datas]$ unset A
变量名称可以由字母,数字和下划线组成,不能以数字开头,环境变量名建议大写
等号两侧不能有空格
在bash中,变量默认类型都是字符串类型,无法直接进行数值运算
变量的值如果有空格,需要使用双引号或单引号括起来
[azula@localhost datas]$ str="A B C"
[azula@localhost datas]$ echo $str
A B C
可把变量提升为全局变量,可供其他shell程序使用
export 变量
#!/bin/bash
echo "$0 $1 $2 $3"
功能描述:获取所有输入参数个数,常用于循环
#!/bin/bash
echo "$0 $1 $2 $3"
echo $#
[azula@localhost datas]$ bash parameter.sh test1 test2
parameter.sh test1 test2
2
test1 test2
test1 test2
expr +,-,\*,/,%
<=> 加减乘除取余# 计算2+3
[azula@localhost datas]$ expr 2 + 3
5# 计算(2+3)*4
## 方式1
[azula@localhost datas]$ expr `expr 2 + 3` \* 4
20## 方式2
[azula@localhost datas]$ s=$[(2+3)*4]
[azula@localhost datas]$ echo $s
20
基本语法:[condition] (注意 condition前后要有空格)
符号 | 描述 |
---|---|
-lt | (less than)小于 |
-le | (less equal) 小于等于 |
-eq | (equal)等于 |
-gt | (greater than) 大于 |
-ge | (greater equal) 大于等于 |
-ne | (not equal) 不等于 |
# 判断23是否大于2
[azula@localhost datas]$ [ 23 -gt 2 ]
[azula@localhost datas]$ echo $?
0# 判断helloworld.sh是否有写入权限
[azula@localhost datas]$ [ -w hellowrld.sh ]
[azula@localhost datas]$ echo $?
1# 判断目录中文件是否存在
[azula@localhost datas]$ [ -e /home/azula/datas ]
[azula@localhost datas]$ echo $?
0
&& ||
[azula@localhost datas]$ cat if.sh
#!/bin/bash
if [ $1 -eq 1 ]
thenecho "awesome"
elif [ $1 -eq 2 ]
thenecho "owwwwww"
fi
[azula@localhost datas]$ bash if.sh 2
owwwwww
[azula@localhost datas]$ cat case.sh
#!/bin/bash
case $1 in
1)echo "firenation"
;;
2)echo "watertribe"
;;
3)echo "earthnation"
;;
esac
[azula@localhost datas]$ bash case.sh 2
watertribe
[azula@localhost datas]$ cat for.sh
#!/bin/bash
s=0
for((i=1;i<=100;i++))
dos=$[$s+$i]
done
echo $s
[azula@localhost datas]$ bash for.sh
5050
[azula@localhost datas]$ cat for2.sh
#!/bin/bash
for i in $*
doecho $i
done
[azula@localhost datas]$ bash for2.sh 1 2
1
2
[azula@localhost datas]$ cat while.sh
#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
dos=$[$s + $i]i=$[$i + 1]
done
echo $s[azula@localhost datas]$ bash while.sh
5050
read (选项) (参数)
# 提示7秒内,读取控制台输入的名称
[azula@localhost datas]$ cat read.sh
#!/bin/bash
read -t 7 -p "在7s内请输入你的名字" NAME
echo $NAME
[azula@localhost datas]$ bash read.sh
在7s
# 方式1
[azula@localhost datas]$ basename /home/azula/a.txt
a.txt# 方式2
[azula@localhost datas]$ basename /home/azula/a.txt .txt
a
[azula@localhost datas]$ dirname /home/azula/banzhang.txt
/home/azula
# 格式
[ function ] funname[()]
{Action:[return int;]
}
funname
# DESC 计算输入两个参数的值
[azula@localhost datas]$ cat sum.sh
#!/bin/bash
function sum(){s=0;s=$[$1 + $2]echo $s
}
read -p "input your param1:" P1
read -p "input your param2:" P2
sum $P1 $P2
[azula@localhost datas]$ bash sum.sh
input your param1:1
input your param2:2
3
cut命令从文件的每一行剪切字节,字符和字段并将这些字节,字符和字段输出
cut [选项参数] filename
# DESC 切割cut.txt第一列
[azula@localhost datas]$ cat cut.txt
dong shen
guan zhen
wo wo
lai lai
le le
[azula@localhost datas]$ cut -d " " -f 1 cut.txt
dong
guan
wo
lai
le# DESC 获取第三行第一个单词
[azula@localhost datas]$ cat cut.txt | grep guan | cut -d " " -f 1
guan
sed是一种流编辑器,它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,成为“模式空间”,接着sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕,接着处理下一行,这样不断重复,知道文件末尾,文件内容并没有改变,除非你使用重定向存储输出
sed [选项参数] 'command' filename
命令功能描述
# DESC 在第二行后增加mei nv字符
[azula@localhost datas]$ sed -e "2a mei nv" sed.txt
dong zhen
guan zhen
mei nv
wo wo
lai lai
awk 一个强大文件分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行分析处理
awk [选项参数] 'pattern1 {action1} pattern2{action2}...' filename
sort:将文件进行排序,并将排序结果标准输出
sort [选项] (参数)
参数 | 描述 |
---|---|
-n | 依照数值大小排序 |
-t | 以相反的顺序排序 |
-t | 设置排序时使用的分隔字符 |
-k | 指定需要排序的列 |
上一篇:C/C++KTV点歌系统
下一篇:@计算矩阵的特征值与特征向量