[英]Run find command from a bash file
Hi people: I'm making a xfe script to take a given directory as source file, use zenity to get output dir and perform some operations, for example: 大家好:我正在制作一个xfe脚本,将给定目录作为源文件,使用zenity获取输出目录并执行一些操作,例如:
#!/bin/bash
OUTPUT_DIR=`zenity --file-selection --directory --filename="$1"`
if [ $? == 0 ]; then
find . -maxdepth 1 -type f -name "*.wav" -exec bash -c 'oggenc -Q "$0" -q 3 "$OUTPUT_DIR/${0%.wav}.ogg"' {} \;
fi
When the script is invoked, oggenc is not executed...any ideas? 调用脚本时,oggenc没有被执行......任何想法?
Solution : Based on answers bellow, this works as expected: 解决方案 :根据下面的答案,这可以按预期工作:
#!/usr/bin/sh
OUTPUT_DIR=$(zenity --file-selection --directory --filename="$1")
if [ $? = 0 ]; then
export OUTPUT_DIR
find "$1" -maxdepth 1 -type f -name "*.wav" -exec sh -c 'oggenc -Q "$0" -q 3 -o "${OUTPUT_DIR}/$(basename "${0/.wav/.ogg}")"' {} \;
fi
zenity --info --text="Done"
To make the variable $OUTPUT_DIR
available to the child process, add one line: 要使变量
$OUTPUT_DIR
可用于子进程,请添加一行:
OUTPUT_DIR=$(zenity --file-selection --directory --filename="$1")
if [ $? = 0 ]; then
export OUTPUT_DIR
find . -maxdepth 1 -type f -name "*.wav" -exec bash -c 'oggenc -Q "$0" -q 3 "$OUTPUT_DIR/${0%.wav}.ogg"' {} \;
fi
Or, slightly simpler: 或者,稍微简单一些:
if OUTPUT_DIR=$(zenity --file-selection --directory --filename="$1"); then
export OUTPUT_DIR
find . -maxdepth 1 -type f -name "*.wav" -exec bash -c 'oggenc -Q "$0" -q 3 "$OUTPUT_DIR/${0%.wav}.ogg"' {} \;
fi
Notes: 笔记:
The command 'oggenc -Q "$0" -q 3 "$OUTPUT_DIR/${0%.wav}.ogg"'
appears in single-quotes. 命令
'oggenc -Q "$0" -q 3 "$OUTPUT_DIR/${0%.wav}.ogg"'
出现在单引号中。 This means that the variables are not expanded by the parent shell. 这意味着父shell不会扩展变量。 They are expanded by the child shell.
它们由子shell扩展。 To make it available to the child shell, a variable must be exported.
要使其可用于子shell,必须导出变量。
[ $? == 0 ]
[ $? == 0 ]
works in bash but [ $? = 0 ]
[ $? == 0 ]
在bash中工作,但[ $? = 0 ]
[ $? = 0 ]
will also work and is more portable. [ $? = 0 ]
也可以工作,更便携。
Command substitution can be done with backticks and some old shells only accept backticks. 命令替换可以用反引号完成,而一些旧shell只接受反引号。 For modern shells, however, the
$(...)
has the advantage of improved readability (some fonts don't clearly distinguish back and normal quotes). 但是,对于现代shell,
$(...)
具有提高可读性的优点(某些字体无法清楚地区分回引号和正常引号)。 Also $(...)
can be nested in a clear and sensible way. $(...)
也可以以清晰明智的方式嵌套。
I'd prefer to use while
loop over pipelining. 我更喜欢使用
while
循环while
不是流水线。 Your code may be rewritten in this way 您的代码可能会以这种方式重写
while IFS= read -r -d '' file; do
oggenc -Q "${file}" -q 3 "${OUTPUT_DIR}/$(basename ${file/.wav/.ogg})"
done < <(find . -maxdepth 1 -type f -name "*.wav" -print0)
The reason why your code was not working is that single quotes '
forbids variables expansion so $OUTPUT_DIR
will not expand. 您的代码无效的原因是单引号
'
禁止变量扩展,因此$OUTPUT_DIR
不会扩展。
EDIT 编辑
-print0
is used in conjunction with IFS=
is to split find
output only on \\0
but not on whitespace in filenames. -print0
与IFS=
一起使用,是仅在\\0
而不是在文件名中的空格上分割find
输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.