当前位置: 技术问答>linux和unix
求教一道shell编程题。
来源: 互联网 发布时间:2015-03-22
本文导语: 要求: (1)将指定目录中的文件备份,要求两个参数:目录名($DIR)和备份文件名($BACKUP); (2)检查目录名是否为/bin,/usr,/sbin等,如果是,则不进行备份; (3) 江北份文件放于/usr/backup/$dir下 提...
要求:
(1)将指定目录中的文件备份,要求两个参数:目录名($DIR)和备份文件名($BACKUP);
(2)检查目录名是否为/bin,/usr,/sbin等,如果是,则不进行备份;
(3) 江北份文件放于/usr/backup/$dir下
提示 :备份文件用tar 命令和gzip命令 ,要求参数正确。
(1)将指定目录中的文件备份,要求两个参数:目录名($DIR)和备份文件名($BACKUP);
(2)检查目录名是否为/bin,/usr,/sbin等,如果是,则不进行备份;
(3) 江北份文件放于/usr/backup/$dir下
提示 :备份文件用tar 命令和gzip命令 ,要求参数正确。
|
#!/bin/sh
if [ $# -ne 2 ]; then
echo "Usage: `basename "$0"` "
exit 1
fi
DIR="$1"
BACKUP="$2"
OLD_DIR=`pwd`
RESTRICTS="/bin /usr /sbin"
for RESTRICT in $RESTRICTS; do
cd "$DIR"
CUR_DIR=`pwd`
if [ $RESTRICT = "$CUR_DIR" ]; then
echo "$DIR is specal directory! You CAN'T backup it!"
exit 1
fi
cd "$OLD_DIR"
done
if [ ! -d "/usr/backup" ]; then
echo "Directory /usr/backup doesn't exist! Now create it!"
mkdir /usr/backup
if [ $? -ne 0 ]; then
echo "Creating directory /usr/backup failed!"
exit 1
fi
fi
echo "Backup ..."
tar zcvf "/usr/backup/`basename $BACKUP`" "$DIR"
echo "... done"
注意:
1. 由于用户可能执行的命令行为:
./backup.sh "~/my doc" "my doc"
目录和文件名中可能含有空白字符,所以引用$DIR、$BACKUP等变量时应注意加上引号;
2. 由于用户可能执行的命令行为:
./backup.sh "../cgi" "cgi"
目录中可能含有.、..以及~等目录符号,所以必须用pwd的类似方法求得其绝对路径,才能与/bin、/usr、/sbin等比较;
3. 由于用户可能执行的命令行为:
./backup.sh "cgi" "/root/cgi"
文件命中含有路径,所以最好用basename命令将之去掉;
4. 由于/usr/backup目录可能不存在,所以在不存在时必须创建它;创建时可能失败,如在/usr中存在一个backup文件时;
5. tar的-z选项会自动调用gzip。
if [ $# -ne 2 ]; then
echo "Usage: `basename "$0"` "
exit 1
fi
DIR="$1"
BACKUP="$2"
OLD_DIR=`pwd`
RESTRICTS="/bin /usr /sbin"
for RESTRICT in $RESTRICTS; do
cd "$DIR"
CUR_DIR=`pwd`
if [ $RESTRICT = "$CUR_DIR" ]; then
echo "$DIR is specal directory! You CAN'T backup it!"
exit 1
fi
cd "$OLD_DIR"
done
if [ ! -d "/usr/backup" ]; then
echo "Directory /usr/backup doesn't exist! Now create it!"
mkdir /usr/backup
if [ $? -ne 0 ]; then
echo "Creating directory /usr/backup failed!"
exit 1
fi
fi
echo "Backup ..."
tar zcvf "/usr/backup/`basename $BACKUP`" "$DIR"
echo "... done"
注意:
1. 由于用户可能执行的命令行为:
./backup.sh "~/my doc" "my doc"
目录和文件名中可能含有空白字符,所以引用$DIR、$BACKUP等变量时应注意加上引号;
2. 由于用户可能执行的命令行为:
./backup.sh "../cgi" "cgi"
目录中可能含有.、..以及~等目录符号,所以必须用pwd的类似方法求得其绝对路径,才能与/bin、/usr、/sbin等比较;
3. 由于用户可能执行的命令行为:
./backup.sh "cgi" "/root/cgi"
文件命中含有路径,所以最好用basename命令将之去掉;
4. 由于/usr/backup目录可能不存在,所以在不存在时必须创建它;创建时可能失败,如在/usr中存在一个backup文件时;
5. tar的-z选项会自动调用gzip。
|
if [ $# != 2 ]
then
echo $0
exit -1
fi
DIR=$1
BACKUP=$2
if [ $DIR -eq "/bin" ] || [ $DIR -eq "/usr" ] || [ $DIR -eq "/sbin" ]
then
echo $DIR is specal directory! Cannot backup it!
else
tar cvf $DIR/$BACKUP /usr/backup/$DIR
fi
then
echo $0
exit -1
fi
DIR=$1
BACKUP=$2
if [ $DIR -eq "/bin" ] || [ $DIR -eq "/usr" ] || [ $DIR -eq "/sbin" ]
then
echo $DIR is specal directory! Cannot backup it!
else
tar cvf $DIR/$BACKUP /usr/backup/$DIR
fi
|
if [ $# != 2 ]
then
echo $0
exit -1
fi
DIR=$1
BACKUP=$2
RESTRICT="/bin /usr /sbin"
for NAME in $RESTRICT
do
if [ $DIR = $NAME ]
then
echo $DIR is specal directory! Cannot backup it!
exit -1
fi
done
echo "backuping..."
tar cvf $DIR/$BACKUP /usr/backup/$DIR
gzip /usr/backup/$DIR/${BACKUP}.tar
then
echo $0
exit -1
fi
DIR=$1
BACKUP=$2
RESTRICT="/bin /usr /sbin"
for NAME in $RESTRICT
do
if [ $DIR = $NAME ]
then
echo $DIR is specal directory! Cannot backup it!
exit -1
fi
done
echo "backuping..."
tar cvf $DIR/$BACKUP /usr/backup/$DIR
gzip /usr/backup/$DIR/${BACKUP}.tar
|
小小改动
# 去除尾部的/
DIR=${1%/}
# 如果不是绝对路径,在前面加上PWD
if [[ $DIR != /* ]] ; then
DIR="$PWD$DIR"
fi
# cvf - options
# tar [options] output.tar
tar cvf /usr/backup/$DIR/$BACKUP.tar $DIR
# 去除尾部的/
DIR=${1%/}
# 如果不是绝对路径,在前面加上PWD
if [[ $DIR != /* ]] ; then
DIR="$PWD$DIR"
fi
# cvf - options
# tar [options] output.tar
tar cvf /usr/backup/$DIR/$BACKUP.tar $DIR