Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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
如何在bash编程中为文件“.txt”创建异常?_Bash_For Loop_Exception_Jpeg - Fatal编程技术网

如何在bash编程中为文件“.txt”创建异常?

如何在bash编程中为文件“.txt”创建异常?,bash,for-loop,exception,jpeg,Bash,For Loop,Exception,Jpeg,因此,当我运行脚本时,问题是它工作得非常好,但我只需要我的程序识别扩展名为“.jpg”的文件,除了其他扩展名不同的文件: 这就是代码: for i in $( cat list.txt );do separate #create a div during the execution addtags $i echo "tags added [ $i ]" done 以及错误输出: Error: Writing of this type of file is not suported - li

因此,当我运行脚本时,问题是它工作得非常好,但我只需要我的程序识别扩展名为“.jpg”的文件,除了其他扩展名不同的文件:

这就是代码:

for i in $( cat list.txt );do

separate #create a div during the execution

addtags $i
echo "tags added [ $i ]"
done
以及错误输出:

Error: Writing of this type of file is not suported - list.txt
PD:'addtags'只支持.jpg'文件。

首先,不要使用for循环,请参见此处。其次,可以使用bash的条件命令将i与模式*.jpg匹配,拒绝那些不匹配的文件

while IFS= read -r i; do
  [[ $i == *.jpg ]] || continue
  addtags "$i"
  echo "tags added [ $i ]"
done < file.txt
使用grepor grep-v选择不需要的文件:

while IFS= read -r file; do
  addtags "${file}"
  echo "tags added [ ${file} ]"
done < <(grep ".jpg$" file.txt)

所以,如果扩展名是txt,您想从list.txt中跳过文件名吗?另外,list.txt是否仅仅是一个文件名列表,每行一个?另一个好链接:。