简体   繁体   English

使用重定向在文件中写入变量的内容在 Bash 脚本的 for 循环中不起作用?

[英]Using redirection to write the contents of a variable in a file is not working in a for loop in Bash script?

#!/bin/bash

files=$( grep " jane " ../data/list.txt)

for f in $files; 
do 
    if test -e f; 
    then
        echo f >> oldFiles.txt;
    else
        echo "File doesn't exist";
    fi;
done

This code searches list.txt's file contents for "jane" and store results in files.此代码在 list.txt 的文件内容中搜索“jane”并将结果存储在文件中。 This file is full of directory path addresses.该文件充满了目录路径地址。 Some of the directory path addresses include the word "jane".一些目录路径地址包括单词“jane”。

After that, the for loop iterates over the files variable files, and one-by-one runs a test to check if the directory path is valid and actually does exist.之后,for 循环遍历 files 变量 files,并逐个运行测试以检查目录路径是否有效并且确实存在。

If the directory path exists, it copies the address in the iterator using redirection method into the file oldFiles.txt如果目录路径存在,则使用重定向方法将迭代器中的地址复制到文件oldFiles.txt中

When I do not create a file to run this script, and instead just use CLI directly to declare the 'files' variable and use the redirection method, it works.当我不创建文件来运行此脚本,而是直接使用 CLI 来声明“文件”变量并使用重定向方法时,它可以工作。 So, for example, when I enter the following in CLI directly:因此,例如,当我直接在 CLI 中输入以下内容时:

files=$( grep " jane " ../data/list.txt) 

echo $files >> oldFiles.txt

This does the job.这可以完成工作。 But the Bash script above does not work.但是上面的 Bash 脚本不起作用。 What am I doing wrong?我究竟做错了什么?

#!/bin/bash

files=$( grep " jane " ../data/list.txt)

for f in $files; 
do 
    if test -e f; 
    then
        echo f >> oldFiles.txt;
    else
        echo "File doesn't exist";
    fi;
done

This code searches list.txt's file contents for "jane" and store results in files.此代码在 list.txt 的文件内容中搜索“jane”并将结果存储在文件中。 This file is full of directory path addresses.该文件充满了目录路径地址。 Some of the directory path addresses include the word "jane".一些目录路径地址包括单词“jane”。

After that, the for loop iterates over the files variable files, and one-by-one runs a test to check if the directory path is valid and actually does exist.之后,for 循环遍历 files 变量 files,并逐个运行测试以检查目录路径是否有效并且确实存在。

If the directory path exists, it copies the address in the iterator using redirection method into the file oldFiles.txt如果目录路径存在,则使用重定向方法将迭代器中的地址复制到文件oldFiles.txt中

When I do not create a file to run this script, and instead just use CLI directly to declare the 'files' variable and use the redirection method, it works.当我不创建文件来运行此脚本,而是直接使用 CLI 来声明“文件”变量并使用重定向方法时,它可以工作。 So, for example, when I enter the following in CLI directly:因此,例如,当我直接在 CLI 中输入以下内容时:

files=$( grep " jane " ../data/list.txt) 

echo $files >> oldFiles.txt

This does the job.这可以完成工作。 But the Bash script above does not work.但是上面的 Bash 脚本不起作用。 What am I doing wrong?我究竟做错了什么?

#!/bin/bash

files=$( grep " jane " ../data/list.txt)

for f in $files; 
do 
    if test -e f; 
    then
        echo f >> oldFiles.txt;
    else
        echo "File doesn't exist";
    fi;
done

This code searches list.txt's file contents for "jane" and store results in files.此代码在 list.txt 的文件内容中搜索“jane”并将结果存储在文件中。 This file is full of directory path addresses.该文件充满了目录路径地址。 Some of the directory path addresses include the word "jane".一些目录路径地址包括单词“jane”。

After that, the for loop iterates over the files variable files, and one-by-one runs a test to check if the directory path is valid and actually does exist.之后,for 循环遍历 files 变量 files,并逐个运行测试以检查目录路径是否有效并且确实存在。

If the directory path exists, it copies the address in the iterator using redirection method into the file oldFiles.txt如果目录路径存在,则使用重定向方法将迭代器中的地址复制到文件oldFiles.txt中

When I do not create a file to run this script, and instead just use CLI directly to declare the 'files' variable and use the redirection method, it works.当我不创建文件来运行此脚本,而是直接使用 CLI 来声明“文件”变量并使用重定向方法时,它可以工作。 So, for example, when I enter the following in CLI directly:因此,例如,当我直接在 CLI 中输入以下内容时:

files=$( grep " jane " ../data/list.txt) 

echo $files >> oldFiles.txt

This does the job.这可以完成工作。 But the Bash script above does not work.但是上面的 Bash 脚本不起作用。 What am I doing wrong?我究竟做错了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM