压缩文件夹 /home/test
tar -cvf aaa.tar test
gzip aaa.tar
或者
tar -c test |gzip >aaa.tar.gz
DIRS="/etc /var /your_directories_or_files"
BACKUP="/tmp/backup.tgz"
tar -c $DIRS | gzip -9 > $BACKUP
其中DIRS放的是您要备份的档案及目录,BACKUP是您的备份档。可不要将/tmp放进DIRS中,那样做,您是在做备份的备份,可能将您的硬碟塞爆。
#######################################
### 发现: aaa.tar.gz等价于aaa.tgz,文件图标上有: tgz,而将其名称改为aaa后,文件图标为: gz,而且不能解压了,说明文件名称的所谓后缀名对linux还是有用的
###
#######################################
循环:
WORD="a b c d e f g h i j l m n o p q r s t u v w x y z"
for i in $WORD
do
echo $i
done
这个Script将会显示a到z。
判断:
if [ $SYSTEM = "Linux" ] ; then
echo "Linux"
elif [ $SYSTEM = "FreeBSD" ] ; then
echo "FreeBSD"
elif [ $SYSTEM = "Solaris" ] ; then
echo "Solaris"
else
echo "What?"
fi
注意:
if
then
elif
then
else
fi
一定不能少了"then"
位置参数(Positional Parameters)
*************************
所谓的位置参数便是0,1,2,3,4,5,6,7,8,9...。使用时,用$0,$1,$2...。
位置参数是当script被载入时,后面所附加的参数。$0是本身,$1则为第一个参数,$2为第二个,依此类推。而当Positional Parameters被function所使用时,它们会被暂时取代(下一节会介绍function)。
例如以下这个script:
#!/bin/sh
# Filename : position
echo $0
echo $1
执行时:
[foxman@foxman bash]# ./position abc
./position
abc
当位置参数超过两位数时,有特别的方法来展开,称为Expansion。
*************************
特殊参数(Speical Parameters)
这些符号,非常不人性,对新手来说很困扰。但上手后,会觉得方便无比,有些如果您看不懂的话,就--算了,不用浪费太多时间在上面。
*************************
* 星号
将Positional Parameters合成一个参数,其间隔为IFS内定参数的第一个字元(见内建变数一节)。
例:
#!/bin/sh
# starsig
echo $*
执行:
[foxman@foxman bash]# starsig a b c d e f g
a b c d e f g
*************************
@ at符号
与*星号类同。不同之处在于不参照IFS。
例:
#!/bin/sh
# atsig
echo $@
执行:
[foxman@foxman bash]# atsig a b c d e f g
a b c d e f g
*************************
# 井字号
展开Positional parameters的数量。
例:
#!/bin/sh
# poundsig
echo $#
执行
[foxman@foxman bash]# poundsig a b c d e f g
7
*************************
? 问号
最近执行的foreground pipeline的状态。
*************************
- 减号
最近执行的foreground pipeline的选项参数。
*************************
$ 钱钱钱
本身的Process ID。
[foxman@foxman bash]# ps ax | grep bash
1635 p1 S 0:00 /bin/bash
[foxman@foxman bash]# echo $$
1635
*************************
! 惊号
最近执行背景命令的Process ID。
*************************
0 零
在Positional Parameters一部份已经说明过了,是执行的shell script本身。但如果是用"bash -c",则$0被设为第一个参数。
[foxman@foxman bash]# echo $0
/bin/bash
*************************
_ 底线符号
显示出最后一个执行的命令。
[foxman@foxman bash]# echo $_
bash
*************************
ftpwho 查看ftp当前连接
tar -cvf aaa.tar test
gzip aaa.tar
或者
tar -c test |gzip >aaa.tar.gz
DIRS="/etc /var /your_directories_or_files"
BACKUP="/tmp/backup.tgz"
tar -c $DIRS | gzip -9 > $BACKUP
其中DIRS放的是您要备份的档案及目录,BACKUP是您的备份档。可不要将/tmp放进DIRS中,那样做,您是在做备份的备份,可能将您的硬碟塞爆。
#######################################
### 发现: aaa.tar.gz等价于aaa.tgz,文件图标上有: tgz,而将其名称改为aaa后,文件图标为: gz,而且不能解压了,说明文件名称的所谓后缀名对linux还是有用的
###
#######################################
循环:
WORD="a b c d e f g h i j l m n o p q r s t u v w x y z"
for i in $WORD
do
echo $i
done
这个Script将会显示a到z。
判断:
if [ $SYSTEM = "Linux" ] ; then
echo "Linux"
elif [ $SYSTEM = "FreeBSD" ] ; then
echo "FreeBSD"
elif [ $SYSTEM = "Solaris" ] ; then
echo "Solaris"
else
echo "What?"
fi
注意:
if
then
elif
then
else
fi
一定不能少了"then"
位置参数(Positional Parameters)
*************************
所谓的位置参数便是0,1,2,3,4,5,6,7,8,9...。使用时,用$0,$1,$2...。
位置参数是当script被载入时,后面所附加的参数。$0是本身,$1则为第一个参数,$2为第二个,依此类推。而当Positional Parameters被function所使用时,它们会被暂时取代(下一节会介绍function)。
例如以下这个script:
#!/bin/sh
# Filename : position
echo $0
echo $1
执行时:
[foxman@foxman bash]# ./position abc
./position
abc
当位置参数超过两位数时,有特别的方法来展开,称为Expansion。
*************************
特殊参数(Speical Parameters)
这些符号,非常不人性,对新手来说很困扰。但上手后,会觉得方便无比,有些如果您看不懂的话,就--算了,不用浪费太多时间在上面。
*************************
* 星号
将Positional Parameters合成一个参数,其间隔为IFS内定参数的第一个字元(见内建变数一节)。
例:
#!/bin/sh
# starsig
echo $*
执行:
[foxman@foxman bash]# starsig a b c d e f g
a b c d e f g
*************************
@ at符号
与*星号类同。不同之处在于不参照IFS。
例:
#!/bin/sh
# atsig
echo $@
执行:
[foxman@foxman bash]# atsig a b c d e f g
a b c d e f g
*************************
# 井字号
展开Positional parameters的数量。
例:
#!/bin/sh
# poundsig
echo $#
执行
[foxman@foxman bash]# poundsig a b c d e f g
7
*************************
? 问号
最近执行的foreground pipeline的状态。
*************************
- 减号
最近执行的foreground pipeline的选项参数。
*************************
$ 钱钱钱
本身的Process ID。
[foxman@foxman bash]# ps ax | grep bash
1635 p1 S 0:00 /bin/bash
[foxman@foxman bash]# echo $$
1635
*************************
! 惊号
最近执行背景命令的Process ID。
*************************
0 零
在Positional Parameters一部份已经说明过了,是执行的shell script本身。但如果是用"bash -c",则$0被设为第一个参数。
[foxman@foxman bash]# echo $0
/bin/bash
*************************
_ 底线符号
显示出最后一个执行的命令。
[foxman@foxman bash]# echo $_
bash
*************************
ftpwho 查看ftp当前连接