echo $PATH //打印环境变脸
export PATH="$PATH:/home/user/bin" //添加环境变量
var=1235
length=${#var}//求var的长度
echo $SHELL //查看当前使用的使那种shell
0——stdin 标准输入
1——stdout 标准输出
2——stderr 标准错误
echo "this is a sample text 1" > temp.txt 覆盖原内容
echo "this is a sample text 2" >> temp.txt 文件尾追加
>等于1> ;>>等于1>>
ls + 2> out.txt 错误打印到out.txt
ls + 2> stderr.txt 1>stdout.txt 错误打印到stderr.txt 正确打印到stdout.txt
ls + &> output.txt error和结果都存入output.txt
cat a* | tee out.txt | cat -n
echo "a" | tee - 将stdin作为参数,-代表参数
自定义文件描述符:
只读模式
截断模式
追加模式
< 操作符用于从文件中读取值stdin,> 操作符用于截断模式的文件写入,>> 操作符用于追加模式的文件写入。
echo this is test >input.txt
exec 3<input.txt
cat <&3
exec 4>output.txt
echo newline >&4
cat output.txt
exec 5>>input.txt
echo appended line >&5}
cat input.txt
使用别名:
别名类似于快捷方式,省却输入一串命令的时间
alias new_command='command sequence'
eg: alias install='sudo apt-get install'
放入~/.bashrc 文件中
echo 'alias cmd="cmd seq"' >>~/.bashrc
############################################################
变量
############################################################
变量名是指向一片用于存储数据的内存空间。
变量:局部变量,环境变量(全局变量)。
shell变量是一种弱类型变量
局部变量:在某一个shell中有效
环境变量:可以使用export内建指令到处为环境变量,环境变量可以派生到任意子shell和孙shell,只能由上向下。
bash中预设的环境变量:$BASH $BASH_VERSION $CDPATH $EUID $FUNCNAME $HISTCMD $HISTFILE $HISTFILESIZE $HISTSIZE $HOSTNAME $HOSTTYPE $MACHTYPE $LANG $PWD $OLDPWD $PATH $PS1
变量命名:以字母和下划线开头,后跟字母,数字下划线,区别大小写
变量赋值和取值: 变量名=变量值 ,没有空格,当变量中有空格是需要使用引号括起来(单双都行)
取值变量前加上$,严谨的方法是${},如果变量引用的别的变量的值(a="$b")需要使用双引号括起来,不能使用单引号(单引号会阻止shell解释特殊字符)
虽说shell是弱类型变量,但是应该遵守先申明后使用,规范不容易出错
设置shell:shopt -s -o nounset
取消变量:unset variable_name
取消函数:unset function_name
特殊变量:
位置参数:$0,脚本本身 $1,第一个参数 $2,第二个参数 ${10},第十个参数,超过九个就应该使用{}
脚本或命令的返回值:$?, 永远是上一个命令的返回值
数组:
定义:declare -a array
array[0]=.....
array[1]=.....
declare -a name=('..','..')
name=('...','....')
score=([3]=3 [5]=5 [8]=8)
数组操作:echo ${array[1]}
//列举所有元素
echo ${array[@]}
echo ${array[*]}
//取得数组元素个数
echo ${#array[@]}
echo ${#array[*]}
//截取元素的某一部分,或数组的某一部分
${array[@]:1:2}
${array[2]:0:5}
//连接数组
conn=(${array[@]} ${name[@]})
//替换元素
arr=(${array[@]/helloworld/hellojohn})
//取消数组或元素
unset array[1]
unset array
只读变量:readonly ro=100
变量作用域:不同进程的id的shell默认为一个不同命名空间。
函数内对变量也是可以访问的。
在函数可以使用local 声明局部变量
引用:双引号里的内容特殊字符会被解析,单引号内不会
命令替换:怎样将命令的输出结果赋值给变量,`命令` $(命令)
date_1=`date`
############################################################
if/else
############################################################
if expression; then
command
fi
if expression; then
command
else
command
fi
if expression1; then
command
elif expression2; then
command
elif expression3; then
command
.....
fi
############################################################
case
############################################################
case var in
var1) command ;;
var2) command ;;
var3) command ;;
...
*) command ;;
esac
var1只能是正则表达式或变量
############################################################
循环
############################################################
for variable in (list)
do
command
done
for var in $(seq 1 100)
不带列表的for
for variable
do
command
done
通过调用shell传入参数,但是可读性太差可以使用以下方式代替
for variable in $@ 接受参数
类c的for
for ((expression 1; expression 2;expression3))
do
command
done
while 循环
while expression
do
command
done
while read LINE
do
name=`echo $LINE | awk '{print $1}'`
echo "$name"
done < student_info.txt
cat student_info.txt | while read LINE
do
name=`echo $LINE | awk '{print $1}'`
done
until循环
until expression //直到expression为真时停止
do
command
done
select循环
select menu in (list)
do
command
done
############################################################
函数
############################################################
function name(){
command
command
....
}
name(){
command
command
...
}
函数调用直接 name
自定义函数库
./PATH/TO/LIB
source /PATH/TO/LIB
############################################################
测试
############################################################
测试结构:test expression
[expression]
文件测试:
test file_operator file
[file_operator file]
file_operator是文件测试符,file是文件、目录
b:当文件存在且是个块文件
c:当文件存在且是个字符设备
d:当文件存在且是个目录
e:当文件或目录存在
f:当文件存在且为普通文件
x:当文件存在且为可执行
w:当文件存在且为可写文件
r:当文件存在且为可读文件
l:当文件存在且为连接文件
p:当文件存在且为管道文件
s:当文件存在且大小不为0
S:当文件存在且为socket文件
g:当文件存在且是指SGID
u:当文件存在且设置SUID
k:文件存在且设置sticky属性
G:文件存在且属于有效的用户组
O:文件存在且属于有效用户
file1 -nt file2:1比2新则true
file2 -ot file2:1比2旧则true
字符串测试:
z:为空
n:非空
=:
!=:
>:
<:
整数比较:
eq:等于
gt:大于
lt:小于
ge:大于等于
le:小于等于
ne:不等于
逻辑测试符号和运算符:
!expression:expression为真,则结果为假
expression -a expression:同时为真则为真
-o:一个为真则结果为真
!:取反
&&:与
||:或
############################################################
随笔记录
############################################################
IFS(internal field separator),存储定界符的环境变量,处理csv文件设置IFS=","
读取字符使用read