shell学习笔记

常用文件重定向命令
command > filename 把把标准输出重定向到一个新文件中
command >> filename 把把标准输出重定向到一个文件中(追加)
command 1 > fielname 把把标准输出重定向到一个文件中
command > filename 2>&1 把把标准输出和标准错误一起重定向到一个文件中
command 2 > filename 把把标准错误重定向到一个文件中
command 2 >> filename 把把标准输出重定向到一个文件中(追加)
command >> filename 2>&1 把把标准输出和标准错误一起重定向到一个文件中(追加)
command < filename >filename2 把c o m m a n d命令以f i l e n a m e文件作为标准输入,以f i l e n a m e 2文件
作为标准输出
command < filename 把c o m m a n d命令以f i l e n a m e文件作为标准输入
command << delimiter 把从标准输入中读入,直至遇到d e l i m i t e r分界符
command <&m 把把文件描述符m作为标准输入
command >&m 把把标准输出重定向到文件描述符m中
command <&- 把关闭标准输入



++shell处理命令的顺序
别名—关键词(if,function,while…)—函数—内置命令—可执行文件和脚本

++内置命令用法查询
help command

++内置命令启用关闭
enable -n pwd #关闭pwd命令
enable pwd   #开启pwd命令

++控制通配符(Globbing)
set noglob 或 set -f #关闭通配符,再试试”ls *”看看,呵.
#恢复 set +f

++统计重复行
sort file|uniq -c

++在文件的每一行的行尾,自动添加一个”;”符号
sed ’s/$/;/’ sourefile > targetname

++if then else循环中,如果需要then部分不执行任何指令,
if [ -d $tmp ]
then
:
else
mkdir -p $tmp
fi

++$@等特定shell变量的含义
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数。与位置变量不同,此选项参数可超过9个
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的进程ID号
$@ 与$#相同,但是使用时加引号,并在引号中返回每个参数
$- 显示shell使用的当前选项,与set命令功能相同
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。

++PS1支持的变量说明
默认: [\u@\h \W]\$
\t  当前时间是HH:MM:SS格式
\d  日期是”星期 月 日”格式
\n  换行符
\s   shell的名字,$0的基名
\w  当前工作目录
\W  当前工作目录的基名
\u   当前用户名
\h   主机名
\#   该命令的编号
\!    该命令的历史编号
\$    如果UID是0,则为#,否则是$
#以下为bash 2.x版才支持的
\@   以12小时am/pm格式表示当前时间
\H   主机名
\T   以12小时格式表示当前时间

++取出两个目录中不同的文件
cat dir1.list dir2.list  |sort|uniq -u

++ 使用 seq 来产生循环参数
for a in `seq 80`     #或者 for a in $( seq 80 )  ,与  ” for a in 1 2 3 4 5 … 80 “相同  (少敲了好多字!).
for a in `seq $BEGIN $END`    #传给 “seq” 两个参数, 从第一个参数开始增长, 一直增长到第二个参数为止.
for a in `seq $BEGIN $INTERVAL $END`  #传给 “seq” 三个参数从第一个参数开始增长,并以第二个参数作为增量, 一直增长到第三个参数为止.

++输入时屏幕不显示(比较输入密码)
stty -echo     #关闭屏幕的echo
stty echo      #恢复屏幕原echo

++取得一个正在运行进程的PID
pidof servername
pgrep servername

++在单引号中引入$变量
ls -l $PATH0$i |awk ‘{print $9}’|sed ’s/^/\\\\192.168.1.1\\Lib\\’$i’\\/’

++()和{}区别
(cmds) 在子shell中执行
{cmds}在当前shell中执行

++echo 相关参数
-e 允许解释转义序列  -n 删除输出结果的换行符
转义序列
\a 报警  \b 退格 \n换行 \r回车 \t制表符

++read相关
read -p “how old r u? ” age          #-p可以输出信息
read -p “some words? ” -a words  #-a输入一组字节,需要echo ${words[*]}取出.
read -p “Password: ” -s passwd    #-s 适合输密码,不回显于屏幕

++取得昨天的日期
yesterday=`date -d “1 day ago” +%Y%m%d`

++脚本中实现pause效果
read -n1 -p “Press any key to continue…”

++一个判断yes/no的脚本
#!/bin/bash
echo “please input ‘yes’ or ‘no’”
read answer
if [ "$answer" = "yes" ]; then
echo “yes”
else
echo “no”
fi

++把文件中的不规则数量(N个)的空格字符换成单个空格字符
cat file |sed ’s/ \{2,\}/ /g’
cat file |tr -s ‘ ‘

++如何在编写SHELL显示多个信息,用EOF
cat << EOF
+————————————————————–+
|         === Welcome to Tunoff services ===                   |
+————————————————————–+
EOF

++把一个文件里面一列的字符串都赋值到一个数组里面并调用
# cat test
test-001
test-002
test-003
test-004
# filename=(`cat test|sed -e :a -e ‘$!N;s/\\n/ /;ta’`)
# set|grep filename
filename=([0]=”test-001″ [1]=”test-002″ [2]=”test-003″ [3]=”test-004″)
echo ${filename2[0]}

++for循环中自动生成累加数
for ((i=0; i<100000; i++));
do
echo $i
done









-b file
file exists and is a block device file

-c file
file exists and is a character device file

-d file
file exists and is a directory

-e file
file exists

-f file
file exists and is a regular file

-g file
file exists and has its setgid bit set

-G file
file exists and is owned by the effective group ID

-k file
file exists and has its sticky bit set

-L file
file exists and is a symbolic link

-O file
file exists and is owned by the effective user ID

-p file
file exists and is a pipe or named pipe (FIFO file)

-r file
file exists and is readable

-s file
file exists and is not empty

-S file
file exists and is a socket

-t N
File descriptor N points to a terminal

-u file
file exists and has its setuid bit set

-w file
file exists and is writeable

-x file
file exists and is executable, or file is a directory that can be searched

aliyun活动 https://www.aliyun.com/acts/limit-buy?userCode=re2o7acl
原文地址:https://www.cnblogs.com/wangbin/p/1539458.html