【问题标题】:Creating file with Bash scripting for loop and if test使用 Bash 脚本为循环和 if 测试创建文件
【发布时间】:2020-06-01 14:32:04
【问题描述】:

问题陈述 使用 bash 脚本查找文件 在本节中,您将在脚本目录中编写一个名为 findJane.sh 的脚本。

此脚本应捕获所有“jane”行并将它们存储在另一个名为 oldFiles.txt 的文本文件中。您将使用我们在前面部分中练习的命令来完成脚本。别担心,我们会全程指导您。

导航到 /scripts 目录并创建一个名为 findJane.sh 的新文件。

我的代码


#!/bin/bash


>oldFiles.txt

files=$(grep  " jane " ../data/list.txt | cut -d ' ' -f 3)

for i in $files:do

  do if test -e ~/data/"$i"; then 
     echo "$i" >> OldFiles.txt; 
  else 
     echo "File doesn't exist"; fi
done

现在输出 文件不存在 文件不存在 文件不存在 它不应该打印任何东西 并且 cat oldFiles.txt 应该返回所有名称为 'jane

的文件

我在哪里编码错误

指南 创建文本文件 oldFiles.txt 并确保它为空。这个 oldFiles.txt 文件应该使用用户名“jane”保存文件。

现在,搜索所有包含名称“jane”的行并将文件名保存到变量中。我们称这个变量为文件,我们稍后会在实验室中使用该名称来引用它。

由于文件 list.txt 中的所有文件都不存在于文件系统中,因此请检查 files 变量中存在的文件名是否实际存在于文件系统中。为此,我们将使用我们在上一节中练习的测试命令。

现在,遍历 files 变量并在循环中添加一个测试表达式。如果 files 变量中的项目通过测试,请将其添加/附加到文件 oldFiles.txt。

【问题讨论】:

  • 对于初学者,您需要将设置files的命令括起来,所以files=$(...)。然后,使用-l--files-with-matches 选项来获取文件名。即使这样,如果文件名中有空格,for 循环也会拆分文件名。 (在文件名中添加该空格的人应被指控行为不合人情。)
  • @Jack 只是一个新手请详细说明,你的讽刺很强烈:)。寻找一个明确的答案。
  • 先尝试修复files=$(grep...) 行。
  • 你在正确的轨道上。修复 shellcheck.net 未解决的语法错误。当您在那里发帖时,请务必使用#!/bin/bash 作为第一行(您缺少# 字符)。如果修复语法错误不能解决您的问题,请使用更新的代码编辑您的 Q,这样人们就不会浪费时间评论简单的修复。包括生成的任何错误消息的确切文本。祝你好运。
  • 正如我所说,你不需要| cut ... 的所有东西。只需在grep 上使用-l 选项。

标签: bash for-loop grep


【解决方案1】:

如果你正在尝试做 Qwicklabs 作业,试试这个

#!/bin/bash

> oldFiles.txt

files=$(grep " jane " /home/<Student-id>/data/list.txt | cut -d ' ' -f 3)

for i in $files;do

if test -e ~/$i;then

echo $i>>oldFiles.txt;

else echo "not working"; fi

done

【讨论】:

  • 你的问题不清楚。你想达到什么目的?您遇到什么错误/问题?请阅读本文以获取有关提出好问题的帮助:How to Ask
【解决方案2】:

我也有同样的问题,只是不要通过 $in grep 调用文件

#!/bin/bash

files= grep ' jane ' ../data/list.txt | cut -d ' ' -f 3

for file in files; do

   if  test -e ~/data/$file; then
     echo  $file >> oldFiles.txt;
   else
     echo "File dosen't exsist"; fi
done

并在命令行中创建新的 fie .oldFiles.txt

喜欢 user@linux: ~/program$ ./findJane.sh &gt; oldFiles.txt

代码应该可以工作

【讨论】:

    【解决方案3】:
    #!/bin/bash
    
    >oldFiles.txt
    
    files=$(grep " jane " ../data/list.txt | cut -d ' ' -f 3)
    
    for i in $files; do
    
      if test -e ~/$i; then
        echo $i >> OldFiles.txt; 
      else 
        echo "File doesn't exist"; fi
    done
    

    【讨论】:

      【解决方案4】:
      #!/bin/bash
      
      >oldFiles.txt
      
      files=$(grep " jane " ../data/list.txt | cut -d ' ' -f 3)
      
      for file in $files; do 
        if [ -e $HOME$file ] ; then 
          echo $HOME$file >> oldFiles.txt; 
        fi 
      done
      

      【讨论】:

        【解决方案5】:

        你有一些错别字。这是您示例的“工作”代码

        #!/bin/bash
        
        >oldFiles.txt
        
        files=$(grep " jane " ../data/list.txt | cut -d ' ' -f 3)
        
        for i in $files; do
        
          if test -e ~/data/"$i"; then
            echo "$i" >> OldFiles.txt; 
          else 
            echo "File doesn't exist"; fi
        done
        

        但是,这仅适用于list.txt 不包含任何空格,除了(至少)三列之间的分隔符

        • 它不适用于Jane,因为它不匹配jane
        • 当有更多像jane example.doc这样的空格时它不会工作
        • 它不适用于包含best movies.txt 等空格的文件名

        因为 list.txt 的概念是不好的选择,所以展示如何正确地做是没有意义的

        【讨论】:

          【解决方案6】:
          #!/bin/bash
          > oldFiles.txt
          files=$(grep ' jane ' ../data/list.txt | cut -d ' ' -f 3)
          #echo $files
          for file in $files; do
                  if  test -e ~/$file; then
                          echo "File path added !!"
                          echo  $file >> oldFiles.txt;
             fi
          done
          

          【讨论】:

          • 请更正您的降价并提供一些详细信息。
          【解决方案7】:

          您需要检查路径,然后将其附加到 oldFile

          #1/bin/bash
          >oldFiles.txt
          files=$(grep "jane " ../data/list.txt | cut -d' ' -f3)
          for f in $files; do
           if [ -e $HOME$f ];then
              echo $HOME$f >> oldFiles.txt;
           fi
           done
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-01-15
            • 2011-05-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-10-17
            相关资源
            最近更新 更多