3、编写一个Shell脚本,从键盘上接收两个文件名,如果两个文件都存在则交换两个文件的内容,否则应给出出
1个回答
展开全部
下面是代码
#!/bin/bash
if [ -z $1 ]; then
echo "Input file 1 and file 2"
exit
fi
if [ -z $2 ]; then
echo "Input file 2"
exit
fi
if [ ! -f $1 ]; then
echo "$1 doesn't exist."
exit
fi
if [ ! -f $2 ]; then
echo "$2 doesn't exist."
exit
fi
mv $1 /tmp/diff.tmp
mv $2 $1
mv /tmp/diff.tmp $2
rm -rf /tmp/diff.tmp
echo "Finished change file"
下面是运行结果
[wyc@wyc-desktop:bash]$ echo 1.c > 1.c
[wyc@wyc-desktop:bash]$ echo 2.c > 2.c
[wyc@wyc-desktop:bash]$ cat 1.c
1.c
[wyc@wyc-desktop:bash]$ cat 2.c
2.c
[wyc@wyc-desktop:bash]$ ./diff.sh 1.c 2.c
Finished change file
[wyc@wyc-desktop:bash]$ cat 1.c
2.c
[wyc@wyc-desktop:bash]$ cat 2.c
1.c
#!/bin/bash
if [ -z $1 ]; then
echo "Input file 1 and file 2"
exit
fi
if [ -z $2 ]; then
echo "Input file 2"
exit
fi
if [ ! -f $1 ]; then
echo "$1 doesn't exist."
exit
fi
if [ ! -f $2 ]; then
echo "$2 doesn't exist."
exit
fi
mv $1 /tmp/diff.tmp
mv $2 $1
mv /tmp/diff.tmp $2
rm -rf /tmp/diff.tmp
echo "Finished change file"
下面是运行结果
[wyc@wyc-desktop:bash]$ echo 1.c > 1.c
[wyc@wyc-desktop:bash]$ echo 2.c > 2.c
[wyc@wyc-desktop:bash]$ cat 1.c
1.c
[wyc@wyc-desktop:bash]$ cat 2.c
2.c
[wyc@wyc-desktop:bash]$ ./diff.sh 1.c 2.c
Finished change file
[wyc@wyc-desktop:bash]$ cat 1.c
2.c
[wyc@wyc-desktop:bash]$ cat 2.c
1.c
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询