Bash shell脚本,逐行从文本文件中读取文件名,并确认它们存在于两个不同的目录中
我需要从文本文件中读取文件列表,并在unix中的两个不同目录中搜索这些文件,如果不存在,则需要打印文件名Bash shell脚本,逐行从文本文件中读取文件名,并确认它们存在于两个不同的目录中,bash,shell,unix,Bash,Shell,Unix,我需要从文本文件中读取文件列表,并在unix中的两个不同目录中搜索这些文件,如果不存在,则需要打印文件名 #!usr/bin/ksh while read -r filename ; do if [ -e $filename ] || [ -e /demo/bin/$filename ]; then echo "File Found!!!! " else echo "not found $filename" fi done < "$1" 但问题是,如果我在脚本中硬编码文件名,它将显示找
#!usr/bin/ksh
while
read -r filename ; do
if [ -e $filename ] || [ -e /demo/bin/$filename ];
then
echo "File Found!!!! "
else
echo "not found $filename"
fi
done < "$1"
但问题是,如果我在脚本中硬编码文件名,它将显示找到的文件。
但是如果我执行相同的脚本,没有硬编码名称,则显示相同的文件名未找到。
我已将需要搜索的所有文件名存储在不同的文件中,该文件名为abc.txt
我正在执行上面的脚本,比如sh isFileExist.sh abc.txt,我已经创建了一个测试目录和文件来演示如何完成 创建目录dir1和dir2` 检查目录
ls
dir1 dir2
创建文件
touch dir{1..2}/{1..10}.txt
ls dir{1..2}/
dir1/:
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
dir2/:
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
检查文件
touch dir{1..2}/{1..10}.txt
ls dir{1..2}/
dir1/:
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
dir2/:
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
创建文件的内容
printf '%s\n' {1..10}.txt > list_of_files.txt
printf '%s\n' {a..c} >> list_of_files.txt
cat list_of_files.txt
检查文件的内容
printf '%s\n' {1..10}.txt > list_of_files.txt
printf '%s\n' {a..c} >> list_of_files.txt
cat list_of_files.txt
输出是
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
10.txt
a
b
c
脚本中的变量foo是dir1,变量条是dir2
#!/bin/sh
foo=dir1
bar=dir2
while read -r files_in_text; do
if [ ! -e "$foo"/"$files_in_text" ] && [ ! -e "$bar"/"$files_in_text" ]; then
printf 'files %s and %s does not exists!\n' "$foo"/"$files_in_text" "$bar"/"$files_in_text"
else
printf 'files %s and %s does exists.\n' "$foo"/"$files_in_text" "$bar"/"$files_in_text"
fi
done < list_of_files.txt
这与java有什么关系?列表中不包含MSWin行尾吗?也许可以将循环重写如下:“`` for filename in$@do if[-e$filename]|[-e/demo/bin/$filename];然后找到echo文件!!!!else echo未找到$filename fi done``格式对我不起作用atmOfftopic:不!usr/bin/ksh钻头/usr/bin/ksh。当文件名有空格时,您需要像[-e$filename]这样的引号。我遇到的问题是因为我将文本从windows机器复制到linux机器,而问题是因为MsWin行结束。谢谢@choroba的评论。