STDIN 0
STDOUT 1
STDERR 2
> 把STDOUT重定向到文件
2> 把STDERR重定向到文件
&> 把所有输出重定向到文件
find /etc -name passwd
$ find /etc -name passwd > find.out
$ find /etc -name passwd 2> /dev/null
$ find /etc -name passwd > find.out 2> find.err
abclkajdfl > f1.log[执行后里面没有内容]
abclkajdfl 2> f1.log[执行后里面有内容]
重定向可以用来屏蔽屏幕的信息显示
ping -c1 192.168.0.254 >/dev/null[垃圾箱设备]
tr 'a-z' 'A-Z' 小写变大写
邮件:
[root@desktop18 bin]# mail -s hello king
hek
kklajd
lkajdf
.
su - king
mail
cat > f1 【单行重定向】
cat >f1 <<eof 【多行重定向,单独一行eof代表结束】
mail -s mail2 wang <<eof
管道:只能重定向标准输出,STDERR不能通过管道转发
echo xxz|tr [:lower:] [:upper:]
ls /err 2>&1 |tr [:lower:] [:upper:]【无论对错,都作为重定向输入】
grep you mail.txt > f1.txt
who |tee f4[重导向到多个目标,屏幕显示,文件里面也有]
for ID in {1..10};do echo the number is $ID; done
for ID in `seq 1 10`;do echo the number is $ID; done
[root@desktop18 bin]# expr 1 + 3
[root@desktop18 bin]# expr 4 \* 3
v1=100
v2=200
v3=$[v1+v2]
sum.sh:
SUM=0
for ID in {1..100}
do
# SUM=$[SUM+ID]
SUM=`expr $SUM + $ID`
done
echo sum=$SUM
unset ID
unset SUM
userdel.sh:
for name in {2001..2002}
do
userdel -r guest$name
done
unset name
mail.sh:
for NAME in `cat usermail.txt`
do
ADDRESS="$NAME@example18.com"
#MESSAGE='Projects are due today!'
#echo mail.txt |mail -s Reminder $ADDRESS
#mail -s hello $NAME< mail.txt
#(echo "hello $NAME";cat mail.txt )|mail -s hello $NAME
mail -s hello $NAME <<EOF
nihao,$NAME
`cat mail.txt`
$hostname
klkjdf
EOF
done
unset NAME
unset ADDRESS
[注意:EOF不顶行会出错,后面也不能加空格]
echo -e "xxx\c" 不换行
echo -e "xxx\n" 多增加一个换行
cat -A f1可以看出空格【windows里面写的脚本拿到linux下执行,最好检查下脚本,通常多了个^M的东西】
cat -n f1 空行也显示行号
cat -b f1 空行不显示行号
cat -sn f1 压缩相邻的空行
rpm -ivh dos2unix-3.1-37.el6.x86_64.rpm 【windows文本格式转换为unix文本格式】
rpm -ivh unix2dos-2.2-35.el6.x86_64.rpm 【unix格式转换为windows格式】
[root@desktop18 ~]# ifconfig eth0 |head -n 2
eth0 Link encap:Ethernet HWaddr 00:0C:29:88:37:7B
inet addr:192.168.0.18 Bcast:192.168.0.255 Mask:255.255.255.0
[root@desktop18 ~]# ifconfig eth0 |head -n 2|tail -n1
inet addr:192.168.0.18 Bcast:192.168.0.255 Mask:255.255.255.0
cat f1 正向显示内容
tac f1 反向显示内容
grep root /etc/passwd
grep ^root /etc/passwd
grep bash$ /etc/passwd
grep -n root /etc/passwd
grep -v root /etc/passwd
df |cut -c52-54
ifconfig eth0|grep Bcast|cut -d: -f2|cut -dB -f1
df |cut -c52-54|wc -l
netstat -ntupe|wc -l
netstat -nt [tcp only]
sort -t: -k3 -n user.txt
sort -t: -k3 -rn user.txt
df |grep sda|cut -c52-54|sort -rn|head -n1 [unix命令太强了]
df |grep sda|cut -c52-54|sort -n|tail -n1 [unix命令太强了]
uniq f1
uniq -c f1
sort -nu f1
ifconfig eth0|sed -n '2p'
ifconfig eth0|sed '2p'
^$代表空行
sed /^$/d
正则表达式:
[abc]abc中的任意一个
[^abc]除了abc
.任意的单一字符
a* a重复不确定的次数
a\{100\}a 重复100次
a\{100,\}a重复100次以上
a\{100,200}\
ifconfig eth0|sed -n 2p|sed 's/^.*r://'|sed 's/ .*$//'
网络设置
vi /etc/udev/rules.d/70-persistent-net.rules【修改网卡的名称】
DNS1=8.8.8.8
DSN2=8.8.4.4
DSN3=114.114.114.114
service NetworkManager status服务启动的情况下,修改eht0的配置文件夹,然后自动生效,但该服务不稳定
echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
vi ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
MACADDR=00:0C:29:88:37:77
IPADDR=192.168.0.118
NETMASK=255.255.255.0
GATEWAY=192.168.0.200
DNS1=192.168.0.254
DNS2=8.8.8.8
DOMAIN=example.com
ONBOOT=YES
时间同步:
先service ntpd stop
然后ntpdate 192.168.0.254
修改:vi /etc/ntp.conf
然后service ntpd start
然后chkconfig ntpd on
system-config-date
grep root /etc/passwd >f1
ps aux(显示全面信息)
uptime
ps axo comm,nice,pid
nice -n -9 vim f1
renice -9 pid
makewhatis & 后台运行
fg 1 前台运行
ctrl+z 后台停止
bg 1 后台运行
jobs
f1.sh;f2.sh 前台顺序执行
(f1.sh&);(f2.sh&)后台并行执行
调度
at 15:46
root/at.log
at> <EOT> 【ctrl+d】
job 1 at 2014-04-12 15:45
at -c 1 (atq)
at -d 1 (atrm)
at脚本里面不要有标准输出,输出会发邮件给用户(可能很多的垃圾邮件)
crontab -e
0-59 0-24 1-31 1-12 0-7 /root/bin/backup.sh
30 4 * * * /root/bin/backup2.sh
[
*
*/10
9-20
2,10,30
]
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
cat /var/spool/cron/root
cat /var/spool/cron/king
禁止king创建cron任务
vi /etc/cron.deny
echo $?[查看上一个命令执行是否成功,不等于0都是失败]
ping 192.168.0.230 -c 1 &>/dev/null && echo the host is up||echo the host is down
test $v1 -gt $v2
[ $v1 -gt $v2 ]
disk.sh
#!/bin/bash
std=90
use=`df |grep sda|cut -c52-54|sort -rn|head -n1`
test $use -gt $std &>/dev/null && wall disk is full
crontab -e里面添加
* * * * * /root/bin/disk.sh
> /boot/f1[直接rm文件,但空间并没有释放,使用这个命令可以避免不释放的问题]
[ -f /etc/issue ] 文件是否存在
[ -r /etc/issue ] 文件可读否
[ -w /etc/issue ] 文件可写否
source 调用没有执行权限的文件
if(case) (a执行com1,b执行com2,c执行com3)
test.sh
!#/bin/bash
v1=1
case $v1 in
1|3|6)
com1
;;
2|4|8)
com2
;;
7|9|10)
com3
;;
*)
com4
;;
esac
unset v1
answer.sh
#!/bin/bash
read -p "Do you agree?(yes or no):" answer
KK=`echo $answer|tr [:upper:] [:lower:]`
case $KK in
yes|y)
echo "your answer is yes"
;;
no|n)
echo "your answer is no"
;;
*)
echo "you do not answer the question"
esac
STDOUT 1
STDERR 2
> 把STDOUT重定向到文件
2> 把STDERR重定向到文件
&> 把所有输出重定向到文件
find /etc -name passwd
$ find /etc -name passwd > find.out
$ find /etc -name passwd 2> /dev/null
$ find /etc -name passwd > find.out 2> find.err
abclkajdfl > f1.log[执行后里面没有内容]
abclkajdfl 2> f1.log[执行后里面有内容]
重定向可以用来屏蔽屏幕的信息显示
ping -c1 192.168.0.254 >/dev/null[垃圾箱设备]
tr 'a-z' 'A-Z' 小写变大写
邮件:
[root@desktop18 bin]# mail -s hello king
hek
kklajd
lkajdf
.
su - king
cat > f1 【单行重定向】
cat >f1 <<eof 【多行重定向,单独一行eof代表结束】
mail -s mail2 wang <<eof
管道:只能重定向标准输出,STDERR不能通过管道转发
echo xxz|tr [:lower:] [:upper:]
ls /err 2>&1 |tr [:lower:] [:upper:]【无论对错,都作为重定向输入】
grep you mail.txt > f1.txt
who |tee f4[重导向到多个目标,屏幕显示,文件里面也有]
for ID in {1..10};do echo the number is $ID; done
for ID in `seq 1 10`;do echo the number is $ID; done
[root@desktop18 bin]# expr 1 + 3
[root@desktop18 bin]# expr 4 \* 3
v1=100
v2=200
v3=$[v1+v2]
sum.sh:
SUM=0
for ID in {1..100}
do
# SUM=$[SUM+ID]
SUM=`expr $SUM + $ID`
done
echo sum=$SUM
unset ID
unset SUM
userdel.sh:
for name in {2001..2002}
do
userdel -r guest$name
done
unset name
mail.sh:
for NAME in `cat usermail.txt`
do
ADDRESS="$NAME@example18.com"
#MESSAGE='Projects are due today!'
#echo mail.txt |mail -s Reminder $ADDRESS
#mail -s hello $NAME< mail.txt
#(echo "hello $NAME";cat mail.txt )|mail -s hello $NAME
mail -s hello $NAME <<EOF
nihao,$NAME
`cat mail.txt`
$hostname
klkjdf
EOF
done
unset NAME
unset ADDRESS
[注意:EOF不顶行会出错,后面也不能加空格]
echo -e "xxx\c" 不换行
echo -e "xxx\n" 多增加一个换行
cat -A f1可以看出空格【windows里面写的脚本拿到linux下执行,最好检查下脚本,通常多了个^M的东西】
cat -n f1 空行也显示行号
cat -b f1 空行不显示行号
cat -sn f1 压缩相邻的空行
rpm -ivh dos2unix-3.1-37.el6.x86_64.rpm 【windows文本格式转换为unix文本格式】
rpm -ivh unix2dos-2.2-35.el6.x86_64.rpm 【unix格式转换为windows格式】
[root@desktop18 ~]# ifconfig eth0 |head -n 2
eth0 Link encap:Ethernet HWaddr 00:0C:29:88:37:7B
inet addr:192.168.0.18 Bcast:192.168.0.255 Mask:255.255.255.0
[root@desktop18 ~]# ifconfig eth0 |head -n 2|tail -n1
inet addr:192.168.0.18 Bcast:192.168.0.255 Mask:255.255.255.0
cat f1 正向显示内容
tac f1 反向显示内容
grep root /etc/passwd
grep ^root /etc/passwd
grep bash$ /etc/passwd
grep -n root /etc/passwd
grep -v root /etc/passwd
df |cut -c52-54
ifconfig eth0|grep Bcast|cut -d: -f2|cut -dB -f1
df |cut -c52-54|wc -l
netstat -ntupe|wc -l
netstat -nt [tcp only]
sort -t: -k3 -n user.txt
sort -t: -k3 -rn user.txt
df |grep sda|cut -c52-54|sort -rn|head -n1 [unix命令太强了]
df |grep sda|cut -c52-54|sort -n|tail -n1 [unix命令太强了]
uniq f1
uniq -c f1
sort -nu f1
ifconfig eth0|sed -n '2p'
ifconfig eth0|sed '2p'
^$代表空行
sed /^$/d
正则表达式:
[abc]abc中的任意一个
[^abc]除了abc
.任意的单一字符
a* a重复不确定的次数
a\{100\}a 重复100次
a\{100,\}a重复100次以上
a\{100,200}\
ifconfig eth0|sed -n 2p|sed 's/^.*r://'|sed 's/ .*$//'
网络设置
vi /etc/udev/rules.d/70-persistent-net.rules【修改网卡的名称】
DNS1=8.8.8.8
DSN2=8.8.4.4
DSN3=114.114.114.114
service NetworkManager status服务启动的情况下,修改eht0的配置文件夹,然后自动生效,但该服务不稳定
echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
vi ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
MACADDR=00:0C:29:88:37:77
IPADDR=192.168.0.118
NETMASK=255.255.255.0
GATEWAY=192.168.0.200
DNS1=192.168.0.254
DNS2=8.8.8.8
DOMAIN=example.com
ONBOOT=YES
时间同步:
先service ntpd stop
然后ntpdate 192.168.0.254
修改:vi /etc/ntp.conf
然后service ntpd start
然后chkconfig ntpd on
system-config-date
grep root /etc/passwd >f1
ps aux(显示全面信息)
uptime
ps axo comm,nice,pid
nice -n -9 vim f1
renice -9 pid
makewhatis & 后台运行
fg 1 前台运行
ctrl+z 后台停止
bg 1 后台运行
jobs
f1.sh;f2.sh 前台顺序执行
(f1.sh&);(f2.sh&)后台并行执行
调度
at 15:46
root/at.log
at> <EOT> 【ctrl+d】
job 1 at 2014-04-12 15:45
at -c 1 (atq)
at -d 1 (atrm)
at脚本里面不要有标准输出,输出会发邮件给用户(可能很多的垃圾邮件)
crontab -e
0-59 0-24 1-31 1-12 0-7 /root/bin/backup.sh
30 4 * * * /root/bin/backup2.sh
[
*
*/10
9-20
2,10,30
]
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
cat /var/spool/cron/root
cat /var/spool/cron/king
禁止king创建cron任务
vi /etc/cron.deny
echo $?[查看上一个命令执行是否成功,不等于0都是失败]
ping 192.168.0.230 -c 1 &>/dev/null && echo the host is up||echo the host is down
test $v1 -gt $v2
[ $v1 -gt $v2 ]
disk.sh
#!/bin/bash
std=90
use=`df |grep sda|cut -c52-54|sort -rn|head -n1`
test $use -gt $std &>/dev/null && wall disk is full
crontab -e里面添加
* * * * * /root/bin/disk.sh
> /boot/f1[直接rm文件,但空间并没有释放,使用这个命令可以避免不释放的问题]
[ -f /etc/issue ] 文件是否存在
[ -r /etc/issue ] 文件可读否
[ -w /etc/issue ] 文件可写否
source 调用没有执行权限的文件
if(case) (a执行com1,b执行com2,c执行com3)
test.sh
!#/bin/bash
v1=1
case $v1 in
1|3|6)
com1
;;
2|4|8)
com2
;;
7|9|10)
com3
;;
*)
com4
;;
esac
unset v1
answer.sh
#!/bin/bash
read -p "Do you agree?(yes or no):" answer
KK=`echo $answer|tr [:upper:] [:lower:]`
case $KK in
yes|y)
echo "your answer is yes"
;;
no|n)
echo "your answer is no"
;;
*)
echo "you do not answer the question"
esac