Shell it!

shell命令总结

一、数据流导向

1.标准输入 stdin 代码为0,使用< 或者 <<

2.标准输出 stdout 代码为1,使用> 或者 >>

3.标准错误输出 stderr 代码为2,使用2> 或者 2>>

黑洞装置:不需要的信息就输出到/dev/null

将错误信息输出到正确信息:2>&1 或者 &>

cat >hello << "eof"

二、管道符及相关命令

管道符:

| ,在管道符右边,使用减号 - 来指代管道符左边的输出,比如

cat a.txt | paste - b.txt
获取管道命令中的各个返回值
1
2
3
cat a.txt | paste - b.txt
rst1=${PIPESTATUS[0]}
rst2=${PIPESATTUS[1]}
截取命令:

cut -d ‘分割字符’ -f index,index2

cut -c 8-13 #取出第8个字符到第13个字符

grep -cinv ‘搜寻的字符串’ filename #count ignore number reverse

排序命令:

sort -nru #number reverse uniq

cat /etc/passwd |sort -t ':' -k 3  #用冒号分割,用每行中分割后的第三个字符排序

去重 uniq -ic # ignore count

统计 wc -lwm #line word 字符数

双重导向:

last | tee last.list | cut -d “ “ -f 1

字符转换:

tr ‘[a-z]’ ‘[A-Z]’ # 将所有的小写字母替换成大写

sed -i “s/hello/world/g” file

sed -i "/<\/manifest>/,/defaulte remote/d" xml 删除相关行

sed -i '/<\/manifest>/i <project=\"hehe\">' xml 在某一行上面添加一行

sed -i '/<manifest>/a <remote="gerrit.com">' xml 在某一行下面添加一行

1
2
sed -e '2i\asd' a.txt #在第二行前面一行添加asd
sed -e '3a\qwe' a.txt #在第三行后面一行添加qwe

tr -d ‘:’ #删除冒号

col -x #将tab装换成space

join [-ti12] file1 file2 #join默认使用空格分割,并且对比俩个文件第一个字段数据,如果相同,则将俩行数据连成一行,并且自一个字段放在第一个

join -t ':' /etc/passed /etc/shadow

paste [-d] file1 file2

paste /etc/passwd /etc/shadow
paste -d "|" /etc/passwd /etc/shadow #使用竖线做分隔符

expand -t 10 file #用10个空格替换tab键

变量名称切割
1
2
3
4
5
var=hello
echo ${var##*l}  #o
echo ${var#*l}  #lo
echo ${var%l*}  #hel
echo ${var%%l*} #he
分割文件:

split [-bl] file PREFIX

split -b 300k /etc/service
split -l 200 /etc/service
参数代换

xargs [-0epn] command #eof print number

cut -d ":" -f 1 /etc/passwd |head -n 3|xargs -n 1 id
echo '11@22@33' |xargs -d '@' -n 1 echo
行处理

sed -n ‘2,5p’ file

sed -n ‘/last/,5p’ file

添加用户

adduser $username --ingroup sudo --disabled-password --gecos "" && echo "$username:$passwd" |chpasswd;chage -d0 $username

awk合并俩个文件

awk 'NR==FNR{a[NR]=$2;b[NR]=$0;next}{for(i in a){if (a[i]==$1){print b[i]" "$2}}}' hel lo

awk替换第二次出现的文本

awk 'NR==2 {sub ("oldtext","newtext")} 1'

sshpass

ssh-keygen -N "" -C "" -f ~/.ssh/id_rsa

sshpass -p $passwd ssh-copy-id -u $username -i ~$username/.ssh/id_rsa.pub $user@$ip

/dev/urandom

head -n 20 | md5sum | cut -c 1-10

eval
变量的值转化为变量名称
1
2
3
4
5
6
7
8
File1=hello
File2=world
File3=ruok
for i in 1 2 3
do
    file=`eval echo '$'File$i`
    echo $file
done
grep命令取出两个文件中不同的行
1
2
3
4
5
#取出相同的行
grep -wf f1.txt f2.txt

#取出不同的行
grep -wvf f1.txt f2.txt #找出只有f2.txt中存在的行
grep
1
2
3
4
grep -x hello a.txt #找出a.txt中包含且只包含hello的行
grep -c hello a.txt #找出a.txt中包含hello的行数
grep -n hello a.txt #找出a.txt中包含hello的行,并且打印行号
grep -w *hello a.txt #精确搜索a.txt中的包含*hello的行。*不会被认为是任意字符

shell脚本

一、特殊字符变量

$# 参数个数

$0 执行的脚本名称

$1 第一个参数

$@ 参数列表

$* 以一个字符串形式列出所有参数

$$ 当前shell进程的pid

$? 上一个shell命令执行的返回值

二、数值运算

var=$(( 13 % 3))
echo $var
echo "123.123*55.9" |bc

三、test命令

test [-efd]

test [-s] 如果文件存在且不为空

test [-rwx]

test [-eq -gt -lt -ne -ge -le]

test [-nt -ot -ef]

test [-zn]

test [-ao !]

使用[ ]进行判断,替换test,[[ ]]里面可以使用正指表达式

四、条件和循环

五、压缩和解压

tar

-c 压缩 ; -x 解压;-t 查看;-r 追加;-u 更新

-z gzip;-j bz2;-J xz

-v 显示过程

-O 解压到stdout

-f 文件名称

tar -zxvf abc.tar.gz -C /home
tar -zcvf snap.tar.gz /home/snap

六、日期

date +%F-%H-%M-%S

七、交互输入

read -p "please enter your name" name
read -t 5 name #等待5秒后,返回非0状态
read -s passwd #不显示输入内容
cat test|while read line
do
  echo "$line"
done

默认read读取的变量到$REPLY

Linux工具

mount

查看已挂载的文件系统

1
2
3
4
5
fs_spec on fs_file type fs_vfstype (fs_mntopts)
fs_spec:挂载的块设备或远程文件系统
fs_file:文件系统的挂载点
fs_vfstype:文件系统的类型
fs_mntopts:与文件系统相关的更多选项,不同的文件系统其选项也不太一样

pushd popd

pushd会进入某个目录,但是会将进入后的目录存储到dirs中,之后可以用popd回到之前的目录

1
2
3
4
5
6
7
pushd ~/work/
pushd ~/work/office
pushd ~/work/office/job
dirs
popd
popd
popd

samba

修改samb配置文件/etc/samba/smb.conf,添加如下代码

1
2
3
4
5
6
7
8
9
10
[data] #对外的路径
comment=Share Folder with username and password #说明文本
path=/work/ #需要给其他机器挂载的本机目录,需要提交创建
public=yes
writeable=yes
valid users=platform #使用的账户,需要在本机已存在这个用户,并且使用smbpasswd -a platform将这个用户添加到samba用户列表中
create mask=0775
directory mask =0775
available=yes
browseable=yes

当其他机器需要挂载这个目录的时候

1
mount.cifs //172.17.121.110/data /work -o username=platform,password=passwd