Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell脚本UNIX_Shell_Unix - Fatal编程技术网

Shell脚本UNIX

Shell脚本UNIX,shell,unix,Shell,Unix,我们有一个包含少数元素列表的文件,每个元素都必须在包含多个文件夹、子文件夹和文件的目录中进行检查。 若我们发现特殊元素,我们应该将其填充到一个文件中,若它不存在,则必须将其填充到另一个文件中。。。我们如何使用unix shell脚本实现它? 示例:文件1: A. B C D 如果我们在任何文件中发现元素A/B/C/D,则应将其填充到一个名为“present.txt”的文件中,或者填充到“缺席.txt”中。 提前感谢这不是一个代码编写服务,但我没有更好的事要做,我去帮你做了,但老实说,除非你自己尝

我们有一个包含少数元素列表的文件,每个元素都必须在包含多个文件夹、子文件夹和文件的目录中进行检查。 若我们发现特殊元素,我们应该将其填充到一个文件中,若它不存在,则必须将其填充到另一个文件中。。。我们如何使用unix shell脚本实现它? 示例:文件1: A. B C D 如果我们在任何文件中发现元素A/B/C/D,则应将其填充到一个名为“present.txt”的文件中,或者填充到“缺席.txt”中。
提前感谢

这不是一个代码编写服务,但我没有更好的事要做,我去帮你做了,但老实说,除非你自己尝试编写,否则你不会学到很多东西

您没有弄清楚元素文件中每行是一个文件名,还是每行有几个文件名。我的测试输入文件gash.txt包含以下内容:

A B C D
E F G H
I J K L
如果每行一个,那么脚本将更简单。这是:

#!/bin/sh

# Initialise filenames
elements=gash.txt
directory=gash
present=present.txt
absent=absent.txt

# Note that when these are used I enclose them in "quotes"
# This is to guard against embedded spaces in the names

# Zeroise files
> "$present"
> "$absent"

# If you have command-line arguments then save them here
# because I am about to blow them away with the 'set'

# 'read' reads each line into variable 'REPLY' by default
while read 
do
    # This 'set' trick will overwrite the program parameters
    # It will NOT work if the names in $elements have embedded whitespace
    set $REPLY

    # This loops through the command-line arguments by default
    for fname
    do 
        # if you don't know the 'find' command then look at 'man find'
        # Note that 'find' returns 0 even if it didn't find the file
        result=$(find "$directory" -name "$fname")

        # The '-n' test returns true if $result is not empty
        if [[ -n $result ]]
        then
            echo "$fname found"
            echo "$fname" >> "$present"
        else
            echo "$fname not found"
            echo "$fname" >> "$absent"
        fi

    done

done < "$elements"
#/垃圾箱/垃圾箱
#初始化文件名
元素=gash.txt
目录=gash
present=present.txt
缺席=缺席.txt
#请注意,当使用这些时,我将它们括在“引号”中
#这是为了防止名称中嵌入空格
#归零文件
>“$present”
>“$缺席”
#如果您有命令行参数,请将它们保存在此处
#因为我要用“布景”把它们吹走
#默认情况下,“read”将每行读入变量“REPLY”
读书时
做
#此“设置”技巧将覆盖程序参数
#如果$elements中的名称中嵌入了空格,那么它将不起作用
设置$REPLY
#默认情况下,这将遍历命令行参数
为fname
做
#如果您不知道“查找”命令,请查看“手动查找”
#请注意,“find”返回0,即使它没有找到文件
结果=$(查找“$目录”-名称“$fname”)
#如果$result不为空,'-n'测试返回true
如果[-n$result]]
然后
回显“$fname已找到”
回显“$fname”>>“$present”
其他的
回显“$fname未找到”
回显“$fname”>>“$缺席”
fi
完成
完成<“$elements”
一个更复杂的版本将从文件名构建一个模式,并使用一个“find”调用进行搜索,但是生命太短暂了(可能是以后的一个好项目)

请随意提问

您也可以这样做:

文件1包含:

A
B
C
D
代码:

directory="your path"

cat file1 | while read line
do
     res=$(find $directory -name "$line")
     if [[ -n $res ]]
     then
         echo $line >> present.txt
     else
         echo $line >> absent.txt
     fi
done
致意