Bash 在unix中,除了echo(dorm文件)之外,还有没有其他方法可以在条件中使用其他命令

Bash 在unix中,除了echo(dorm文件)之外,还有没有其他方法可以在条件中使用其他命令,bash,if-statement,unix,Bash,If Statement,Unix,好的,我正在从filelist.txt读取文件名;对于filelist.txt中的每一行,我想运行一个命令lar-c 为了测试我的代码,我只想在第一行运行 这是我的密码 #!/bin/bash fileList=/path/to/fileList.txt count=1 if [[ $count == 1]]; then #getting the first line of the txt file line=$(sed "${count}q;d" $fileList) #exec

好的,我正在从filelist.txt读取文件名;对于filelist.txt中的每一行,我想运行一个命令
lar-c

为了测试我的代码,我只想在第一行运行

这是我的密码

#!/bin/bash

fileList=/path/to/fileList.txt
count=1
if [[ $count == 1]]; then
  #getting the first line of the txt file
  line=$(sed "${count}q;d" $fileList)
  #execute my command
  do lar -c $line
fi;
count=`expr $count +1`
但是当我试图执行我的.sh文件时,我得到了以下错误

./ReadFilesFirstBatch.sh: line 34: syntax error near unexpected token `do'

很抱歉,我对bash编码非常陌生,但我想弄清楚这一点

您需要在读取循环时使用

#!/usr/bin/env bash

fileList=/path/to/fileList.txt

while read -ra line; do
  echo lar -c "${line[@]}"
done < "$fileList"
#/usr/bin/env bash
fileList=/path/to/fileList.txt
而read-ra行;做
echo lar-c“${line[@]}”
完成<“$fileList”
  • 使用read时需要-a选项,因此您每行都要构建一个数组,否则您需要
    eval
    ,您不应该使用该选项(至少现在)
  • 如果您对输出感到满意,请删除
    echo
  • 如果要验证脚本,请尝试


    • 这个
      做什么
      应该做什么?简单地写

      lar -c "$line" 
      
      没有
      do
      。不要忘记引号,因为
      包含空格

      顺便说一句,最后一行可以写得更简单

      ((count++))
      

      do
      是bash保留的关键字,while使用该关键字,对于语法中的循环,不确定为什么要使用
      do lar-c$line
      ,这里不需要提及do。有关
      if语句的构造if
      ,请参阅
      help if
      !谢谢你的反馈!