我如何更正从 Unix Bourne Shell 中的目录打印出复制的文件?
How do I correct this printing out copied files from directory in Unix Bourne Shell?
所以我需要编写一个 Unix Bourne Shell 来将名为 dir1 和 dir2 的目录中的所有文件复制到名为 dir3 的新目录中。
脚本首先将目录 1 中的所有文件复制到目录 3 中。
脚本然后根据这些将每个文件从 dir2 复制到 dir3
条件:如果 dir2 中的文件不存在于 dir3 中,或者如果 dir2
文件比同一个 dir3 文件更新,它将覆盖 dir3 中的同名文件。
这些应该是输出:
These files from dir1 copied into dir3:
a.c b.c file1.c
These new file(s) from dir2 copied into dir3:
file2.c
These file(s) from dir2 copied into dir3 and overwrite(s) their
namesakes in dir3:
a.c
$ ls
cp2.sh dir1 dir2 dir3
$ ls -l dir1
total 12
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 a.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 b.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 file1.c
$ ls -l dir2
total 12
-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 a.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:51 b.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 file2.c
$ ls -l dir3
total 16
-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 a.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 b.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 file1.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 file2.c
所以从技术上讲,因为 dir2 a.c 比 dir1 a.c 晚 1 分钟创建,所以它被粘贴进入 dir3,覆盖之前粘贴的 dir1 a.c.
这不适用于 dir2 b.c,因为它早于 dir1 b.c 创建。
然而,我的输出是这样的:
These files from dir1 copied into dir3:
a.c b.c file1.c
These new file(s) from dir2 copied into dir3:
file2.c
These file(s) from dir2 copied into dir3 and overwrite(s) their
namesakes in dir3:
a.c
b.c
这是我的代码:
#!/bin/sh
#Test for directory existence
test -d
if [ $? -eq 1 ]
then
echo "'' directory does not exist."
exit 0
elif
test -d
[ $? -eq 1 ]
then
echo "'' directory does not exist."
exit 0
elif
test -d
[ $? -eq 0 ]
then
echo "'' directory does not exist."
exit 0
fi
#create dir3
mkdir
#output for users
echo "These new file(s) from copied into :"
ls
ls | while read file
do
cp /$file /$file
done
echo "These new file(s) from copied into :"
ls | while read file
do
test -f /$file
if [ $? -eq 1 ]
then
echo "$file"
fi
done
echo "These file(s) from copied into and overwrite(s) their namesakes in :"
ls | while read file
do
test -f /$file
if [ $? -eq 0 ]
then
cp -u /$file /$file
echo "$file"
else
cp /$file /$file
fi
done
我想这些位是错误的,但无法找出问题所在...
echo "These file(s) from copied into and overwrite(s) their namesakes in :"
ls | while read file
do
test -f /$file
if [ $? -eq 0 ]
then
cp -u /$file /$file
echo "$file"
else
cp /$file /$file
fi
done
非常感谢。非常感谢
不要使用一系列 elif
,循环就可以了。只有 return 退出代码 0
当一切正常时,而不是当出现问题时:
for d in "" "" ; do
test -d "$d" || \
{ echo "'$d' directory does not exist."
exit 1 ; }
done
而mkdir
如果目标目录存在则已经return报错,所以没必要写多余的代码。够了:
mkdir "" || exit
除了给用户的输出,文件复制 while
循环并不是真正需要的。只需将 cp
与 -u
或 --update
以及 -v
或 --verbose
开关一起使用;有关详细信息,请参阅 man cp
。两个 while
循环可以一起减少到一行:
cp -uv ""/* ""/* ""
所以我需要编写一个 Unix Bourne Shell 来将名为 dir1 和 dir2 的目录中的所有文件复制到名为 dir3 的新目录中。
脚本首先将目录 1 中的所有文件复制到目录 3 中。
脚本然后根据这些将每个文件从 dir2 复制到 dir3 条件:如果 dir2 中的文件不存在于 dir3 中,或者如果 dir2 文件比同一个 dir3 文件更新,它将覆盖 dir3 中的同名文件。
这些应该是输出:
These files from dir1 copied into dir3:
a.c b.c file1.c
These new file(s) from dir2 copied into dir3:
file2.c
These file(s) from dir2 copied into dir3 and overwrite(s) their
namesakes in dir3:
a.c
$ ls
cp2.sh dir1 dir2 dir3
$ ls -l dir1
total 12
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 a.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 b.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 file1.c
$ ls -l dir2
total 12
-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 a.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:51 b.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 file2.c
$ ls -l dir3
total 16
-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 a.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 b.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 file1.c
-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 file2.c
所以从技术上讲,因为 dir2 a.c 比 dir1 a.c 晚 1 分钟创建,所以它被粘贴进入 dir3,覆盖之前粘贴的 dir1 a.c.
这不适用于 dir2 b.c,因为它早于 dir1 b.c 创建。
然而,我的输出是这样的:
These files from dir1 copied into dir3:
a.c b.c file1.c
These new file(s) from dir2 copied into dir3:
file2.c
These file(s) from dir2 copied into dir3 and overwrite(s) their
namesakes in dir3:
a.c
b.c
这是我的代码:
#!/bin/sh
#Test for directory existence
test -d
if [ $? -eq 1 ]
then
echo "'' directory does not exist."
exit 0
elif
test -d
[ $? -eq 1 ]
then
echo "'' directory does not exist."
exit 0
elif
test -d
[ $? -eq 0 ]
then
echo "'' directory does not exist."
exit 0
fi
#create dir3
mkdir
#output for users
echo "These new file(s) from copied into :"
ls
ls | while read file
do
cp /$file /$file
done
echo "These new file(s) from copied into :"
ls | while read file
do
test -f /$file
if [ $? -eq 1 ]
then
echo "$file"
fi
done
echo "These file(s) from copied into and overwrite(s) their namesakes in :"
ls | while read file
do
test -f /$file
if [ $? -eq 0 ]
then
cp -u /$file /$file
echo "$file"
else
cp /$file /$file
fi
done
我想这些位是错误的,但无法找出问题所在...
echo "These file(s) from copied into and overwrite(s) their namesakes in :"
ls | while read file
do
test -f /$file
if [ $? -eq 0 ]
then
cp -u /$file /$file
echo "$file"
else
cp /$file /$file
fi
done
非常感谢。非常感谢
不要使用一系列 elif
,循环就可以了。只有 return 退出代码 0
当一切正常时,而不是当出现问题时:
for d in "" "" ; do
test -d "$d" || \
{ echo "'$d' directory does not exist."
exit 1 ; }
done
而mkdir
如果目标目录存在则已经return报错,所以没必要写多余的代码。够了:
mkdir "" || exit
除了给用户的输出,文件复制 while
循环并不是真正需要的。只需将 cp
与 -u
或 --update
以及 -v
或 --verbose
开关一起使用;有关详细信息,请参阅 man cp
。两个 while
循环可以一起减少到一行:
cp -uv ""/* ""/* ""