我想迭代一个文件列表.这个列表是find命令的结果,所以我想出了:
getlist() {
for f in $(find . -iname "foo*")
do
echo "File found: $f"
# do something useful
done
}
Run Code Online (Sandbox Code Playgroud)
没关系,除非文件名中有空格:
$ ls
foo_bar_baz.txt
foo bar baz.txt
$ getlist
File found: foo_bar_baz.txt
File found: foo
File found: bar
File found: baz.txt
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能避免空格分裂?
我有以下shell脚本.目的是循环到目标文件的每一行(其路径是脚本的输入参数)并对每一行进行操作.现在,它似乎只适用于目标文件中的第一行,并在该行被处理后停止.我的剧本有什么问题吗?
#!/bin/bash
# SCRIPT: do.sh
# PURPOSE: loop thru the targets
FILENAME=$1
count=0
echo "proceed with $FILENAME"
while read LINE; do
let count++
echo "$count $LINE"
sh ./do_work.sh $LINE
done < $FILENAME
echo "\ntotal $count targets"
Run Code Online (Sandbox Code Playgroud)
在do_work.sh,我运行了几个ssh命令.
我无法运行它,因为Java仅等待ffmpeg。但是ffmpeg不提供输入-也不提供错误流。它只是运行,但是什么也没做。
插入bash的“ System.out.println(” command:..“)的输出按预期运行正常。因此ffmpeg语法没有问题。
这是代码。
package mypackage;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
/**
*
* @author test
*/
public class ffmpeg_hang {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, InterruptedException {
String INPUT_FILE="/path/to/media";
String FFMPEG_PATH="/path/to/ffmpegFolder/";
for(int i=0;(i+4)<40;i+=4){
String[] ffmpeg_pipe = new String[]{
FFMPEG_PATH + "ffmpeg_4.1.1",
"-ss",(i+""),"-t", "4",
"-i", INPUT_FILE,
"-ac", "1", "-acodec", "pcm_s16le", "-ar", "16000",
"-f","nut","-","|",
FFMPEG_PATH + "ffmpeg_4.1.1",
"-i","-",
"-lavfi", "showspectrumpic=s=128x75:legend=disabled:saturation=0:stop=8000",
"-f","image2pipe","pipe:1"};
System.out.println("command: "+String.join(" ", …Run Code Online (Sandbox Code Playgroud) 我有一个bash脚本,我需要遍历find命令的输出的每一行,但看起来我正在从find命令迭代每个Word(空格分隔).到目前为止我的脚本看起来像这样:
folders=`find -maxdepth 1 -type d`
for $i in $folders
do
echo $i
done
Run Code Online (Sandbox Code Playgroud)
我希望这会给出如下输出:
./dir1 and foo
./dir2 and bar
./dir3 and baz
Run Code Online (Sandbox Code Playgroud)
但我得到这样的输出:
./dir1
and
foo
./dir2
and
bar
./dir3
and
baz
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
几年前,当我还年轻、无忧无虑、呃、不太了解编写 shell 脚本的良好实践时;我编写了一个快速而肮脏的脚本来协助完成我面临的任务:
#!/bin/bash
# autohighlighter which generates highlights from a video clip and file
process_highlight_file() {
n=0
while read -r line; do
begin=$(echo "$line" | awk '{ print $1 }' )
end=$(echo "$line" | awk '{ print $2 }')
hilightname=$(echo "$line" | awk '{ print $3 }')
printf "Begin highlight called %s at %s, to %s\n" "$hilightname" "$begin" "$end"
echo "$begin $end"
sleep 2
echo "ffmpeg -y -ss $begin -i $videofile -to $end -c copy -avoid_negative_ts 1 …Run Code Online (Sandbox Code Playgroud)