Board logo

标题: [问题求助] Shell问题请教,参数如何传递 [打印本页]

作者: netdzb    时间: 2019-6-24 10:53     标题: Shell问题请教,参数如何传递

自己写了一半,后续不知道怎么写?

#!/bin/bash
for FILE in *.扩展名;
    do
        命令 "$FILE" 命令参数逐行存在文本里面,需要逐行传递参数
    done

请给予指点,谢谢。
作者: cfwyy77_bat    时间: 2019-6-24 15:46

本帖最后由 cfwyy77_bat 于 2019-6-24 16:08 编辑

下面是按行打印的例子,不知是不是你想要的意思。
  1. #!/bin/bash
  2. #按行分割
  3. IFS=$'\n'
  4. for line in `cat file.txt`
  5. do
  6.     echo $line
  7. done
复制代码
或者 用while +重定向
  1. #!/bin/bash
  2. while read line
  3. do
  4.    echo $line
  5. done < file.txt
复制代码

作者: netdzb    时间: 2019-6-24 17:29

回复 2# cfwyy77_bat

多谢,我去试试看。
作者: netdzb    时间: 2019-6-24 17:59

本帖最后由 netdzb 于 2019-6-24 18:01 编辑

回复 2# cfwyy77_bat

假设文本的book.txt存放的是码率信息,

1000k
2000k
3000k
4000k
5000k

目录下有5个视频文件。

如何通过文本把码率参数传进去,用ffmpeg进行转码呢?
作者: Batcher    时间: 2019-6-25 08:31

回复 4# netdzb


看看还需要加什么参数:
  1. #!/bin/bash
  2. paste <(ls *.mp3) book.txt | while read LINE; do
  3.     echo ffmpeg $LINE
  4. done
复制代码

作者: cfwyy77_bat    时间: 2019-6-25 09:44

回复 4# netdzb
每个视频文件都要转成book.txt中的各种码率?还是说一个文件对应每行的一种码率?
作者: netdzb    时间: 2019-6-25 09:56

回复 6# cfwyy77_bat

book.txt第一行对应第一个视频的码率,第二行对应第二个,依次类推。

下面的脚本有问题,跑不通,帮忙看看。

#!/bin/bash
for FILE in *.mp4;
do
  ffmpeg -i $FILE -c:v libx264 -c:a copy -b:v  paste < book.txt | while read LINE $FILE.*-.mp4
                ~~~~~                                                                                                ~~~~~~~~~~
                输入                                                                                                       输出
done
作者: Batcher    时间: 2019-6-25 10:30

回复 7# netdzb
  1. #!/bin/bash
  2. paste <(ls *.mp3) book.txt | while read LINE; do
  3.     ffmpeg -i $LINE -c:v libx264 -c:a copy -b:v
  4. done
复制代码

作者: netdzb    时间: 2019-6-25 10:46

回复 8# Batcher

跑不通,不知道错在哪里。已经换到unix格式了。


$ bash shi.sh
shi.sh: line 5: syntax error near unexpected token `done'
shi.sh: line 5: `done'

Administrator@USER-20151226SM /i/jiemi
作者: cfwyy77_bat    时间: 2019-6-25 10:56

回复 7# netdzb


你写的那个让人看不懂。

可以试试这个,ffmpeg的参数的写法自己看,我没怎么用过ffmpeg.
  1. #!/bin/bash
  2. file=(`ls *.mp4`)
  3. bitRate=(`cat book.txt`)
  4. for i in `seq 0 1 4`
  5. do
  6.    ffmpeg -i ${file[$i]} -c:v libx264 -c:a copy -b:v ${bitRate[$i]}  ${bitRate[$i]}-${file[$i]}
  7. done
复制代码

作者: netdzb    时间: 2019-6-25 11:03

回复 10# cfwyy77_bat

我的shell好像坏了,居然不认识done。下面的代码会跑不通的,奇怪了。

#!/bin/bash
while read LINE
do
    echo $LINE
done < book.txt
作者: cfwyy77_bat    时间: 2019-6-25 11:09

回复 11# netdzb
写法应该没问题,换行符格式对不对?
作者: netdzb    时间: 2019-6-25 11:13

回复 12# cfwyy77_bat

是换行符的问题,搞定了。
可惜显示的book.txt缺少最后一行,也不知道问题出在哪里?
作者: netdzb    时间: 2019-6-25 12:14

回复 8# Batcher

脚本跑不通,是不是我的shell有问题啊?

控制台直接输入下面
paste < ls    // 命令报错
ls | paste     // 可以通过

上面的2条命令都合法吗?还是第1条命令不合法,第2条命令合法。
作者: Batcher    时间: 2019-6-25 12:20

回复 14# netdzb
  1. paste <(ls *.mp3) book.txt
复制代码
执行这个命令把完整的报错信息发出来看看
另外你用的什么操作系统呢,RHEL? CentOS? Ubuntu? AIX? Solaris? 具体是哪个版本呢
作者: netdzb    时间: 2019-6-25 13:27

回复 15# Batcher

$ paste < (ls *.mp4) book.txt
sh: syntax error near unexpected token `('

我是windows xp环境,装的msys 1.0,看来兼容性还是有问题啊。
作者: cfwyy77_bat    时间: 2019-6-25 15:10

回复 13# netdzb


    看你另开的一个帖子,我的回复。
作者: Batcher    时间: 2019-6-26 13:29

回复 16# netdzb
  1. #!/bin/bash
  2. COUNT=0
  3. for FILE in *.mp3; do
  4.     let COUNT+=1
  5.     # ffmpeg需要的参数你自己添加
  6.     echo ffmpeg $FILE $(sed -n "${COUNT}p" "book.txt")
  7. done
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2