
I am new to Bash scripts. I am trying to create a script to use avconv to convert mkv files to mp4 file. Run one at a time the command runs fine. When I execute this script I get an error about an unknown parameter. I am guessing that the expansion with the filename variables is failing. Can someone help me correct? Thank you! #!/bin/bash # Usage: convert file to mp4 shopt -s nullglob ext='.mp4' for f in *.mkv do base=$(basename $f .mkv) nFname=$base".mp4" echo $nFname avconv "-i $f -c:v libx264 -c:a copy $nFname" done -- Stephen

On 01/07/17 19:25, Stephen via talk wrote:
I am new to Bash scripts.
I am trying to create a script to use avconv to convert mkv files to mp4 file.
Run one at a time the command runs fine.
When I execute this script I get an error about an unknown parameter. I am guessing that the expansion with the filename variables is failing.
Can someone help me correct?
Thank you!
#!/bin/bash # Usage: convert file to mp4 shopt -s nullglob ext='.mp4' for f in *.mkv do base=$(basename $f .mkv) nFname=$base".mp4" echo $nFname avconv "-i $f -c:v libx264 -c:a copy $nFname" done
Shouldn't you be quoting $f and $nFname separately, e.g. avconv -i "$f" -c:v libx264 -c:a copy "$nFname" otherwise filenames with spaces will see -i catch only part of the filename. Try adding echo in front of the avconv command and see what would be executed from within the script. -- Michael Galea

Quotes. I second to that. To see how the script are interpreting things, run bash -vx script.sh. Or install and use bashdb (bash debugger). It's very, very useful. On Jan 7, 2017 22:38, "Michael Galea via talk" <talk@gtalug.org> wrote:
On 01/07/17 19:25, Stephen via talk wrote:
I am new to Bash scripts.
I am trying to create a script to use avconv to convert mkv files to mp4 file.
Run one at a time the command runs fine.
When I execute this script I get an error about an unknown parameter. I am guessing that the expansion with the filename variables is failing.
Can someone help me correct?
Thank you!
#!/bin/bash # Usage: convert file to mp4 shopt -s nullglob ext='.mp4' for f in *.mkv do base=$(basename $f .mkv) nFname=$base".mp4" echo $nFname avconv "-i $f -c:v libx264 -c:a copy $nFname" done
Shouldn't you be quoting $f and $nFname separately, e.g. avconv -i "$f" -c:v libx264 -c:a copy "$nFname" otherwise filenames with spaces will see -i catch only part of the filename.
Try adding echo in front of the avconv command and see what would be executed from within the script.
-- Michael Galea --- Talk Mailing List talk@gtalug.org https://gtalug.org/mailman/listinfo/talk

On 2017-01-07 07:25 PM, Stephen via talk wrote:
I am new to Bash scripts.
I am trying to create a script to use avconv to convert mkv files to mp4 file.
Run one at a time the command runs fine.
When I execute this script I get an error about an unknown parameter. I am guessing that the expansion with the filename variables is failing.
Can someone help me correct?
Thank you!
#!/bin/bash # Usage: convert file to mp4 shopt -s nullglob ext='.mp4' for f in *.mkv do base=$(basename $f .mkv) nFname=$base".mp4" echo $nFname avconv "-i $f -c:v libx264 -c:a copy $nFname" done
Thank you for the replies. Simply removing the quotes did the trick! :) -- Stephen

On the topic of quotes, see my in-line comments... On Sun, Jan 08, 2017 at 05:57:40AM -0500, Stephen via talk wrote:
On 2017-01-07 07:25 PM, Stephen via talk wrote:
#!/bin/bash # Usage: convert file to mp4 shopt -s nullglob ext='.mp4' for f in *.mkv do base=$(basename $f .mkv)
Should be base=$(basename "$f" .mkv)
nFname=$base".mp4"
nFname="$base".mp4
echo $nFname avconv "-i $f -c:v libx264 -c:a copy $nFname"
avconv -i "$f" -c:v libx264 -c:a copy "$nFname"
done
Thank you for the replies. Simply removing the quotes did the trick! :)

Or nFname="${base}.mp4" Using brackets you can mix variables and text and create something like this: newName="${oldname}-bkp-${filedate}.${extension}.old" Mauro http://mauro.limeiratem.com - registered Linux User: 294521 Scripture is both history, and a love letter from God. 2017-01-08 13:21 GMT-02:00 William Park via talk <talk@gtalug.org>:
On the topic of quotes, see my in-line comments...
On Sun, Jan 08, 2017 at 05:57:40AM -0500, Stephen via talk wrote:
On 2017-01-07 07:25 PM, Stephen via talk wrote:
#!/bin/bash # Usage: convert file to mp4 shopt -s nullglob ext='.mp4' for f in *.mkv do base=$(basename $f .mkv)
Should be base=$(basename "$f" .mkv)
nFname=$base".mp4"
nFname="$base".mp4
echo $nFname avconv "-i $f -c:v libx264 -c:a copy $nFname"
avconv -i "$f" -c:v libx264 -c:a copy "$nFname"
done
Thank you for the replies. Simply removing the quotes did the trick! :)
Talk Mailing List talk@gtalug.org https://gtalug.org/mailman/listinfo/talk

Hmm... yes. Now that you mention it, the quoting here is unnecessary, because it's variable assignment and further splitting is not done. -- William On Sun, Jan 08, 2017 at 01:28:03PM -0200, Mauro Souza wrote:
Or nFname="${base}.mp4"
Using brackets you can mix variables and text and create something like this: newName="${oldname}-bkp-${filedate}.${extension}.old"
Mauro http://mauro.limeiratem.com - registered Linux User: 294521 Scripture is both history, and a love letter from God.
2017-01-08 13:21 GMT-02:00 William Park via talk <talk@gtalug.org>:
On the topic of quotes, see my in-line comments...
On Sun, Jan 08, 2017 at 05:57:40AM -0500, Stephen via talk wrote:
On 2017-01-07 07:25 PM, Stephen via talk wrote:
#!/bin/bash # Usage: convert file to mp4 shopt -s nullglob ext='.mp4' for f in *.mkv do base=$(basename $f .mkv)
Should be base=$(basename "$f" .mkv)
nFname=$base".mp4"
nFname="$base".mp4
echo $nFname avconv "-i $f -c:v libx264 -c:a copy $nFname"
avconv -i "$f" -c:v libx264 -c:a copy "$nFname"
done
Thank you for the replies. Simply removing the quotes did the trick! :)
Talk Mailing List talk@gtalug.org https://gtalug.org/mailman/listinfo/talk

On Sun, 8 Jan 2017, Mauro Souza via talk wrote:
Or nFname="${base}.mp4"
Using brackets you can mix variables and text and create something like this: newName="${oldname}-bkp-${filedate}.${extension}.old"
The braces are not necessary here. This will work fine: newName="$oldname-bkp-$filedate.$extension.old" Braces would be necessary if you were using underscores instead of hyphens because they can be part of a variable name: newName="${oldname}_bkp_$filedate.$extension.old" -- Chris F.A. Johnson, <http://cfajohnson.com>

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Chris F.A. Johnson wrote:
The braces are not necessary here.
Are braced incorrect here? I've been putting braces around all variable names, so that there are fewer syntax exceptions for me to remember... - --Bob. On 2017-01-09 05:47 PM, Chris F.A. Johnson via talk wrote:
On Sun, 8 Jan 2017, Mauro Souza via talk wrote:
Or nFname="${base}.mp4"
Using brackets you can mix variables and text and create something like this: newName="${oldname}-bkp-${filedate}.${extension}.old"
The braces are not necessary here. This will work fine:
newName="$oldname-bkp-$filedate.$extension.old"
Braces would be necessary if you were using underscores instead of hyphens because they can be part of a variable name:
newName="${oldname}_bkp_$filedate.$extension.old"
- -- - -- Bob Jonkman <bjonkman@sobac.com> Phone: +1-519-635-9413 SOBAC Microcomputer Services http://sobac.com/sobac/ Software --- Office & Business Automation --- Consulting GnuPG Fngrprnt:04F7 742B 8F54 C40A E115 26C2 B912 89B0 D2CC E5EA -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Ensure confidentiality, authenticity, non-repudiability iEYEARECAAYFAlh0I9wACgkQuRKJsNLM5erWdQCgohmGdSVT2s1DHEWs+3+I3BCn nR4AoM8TVyt53lC3KhHC6uecvy09Zq9z =Af5d -----END PGP SIGNATURE-----

I almost always use them. Only left them out for very simple scripts. It's not really necessary all the time, but better safe than sorry... On Jan 9, 2017 22:00, "Bob Jonkman via talk" <talk@gtalug.org> wrote: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Chris F.A. Johnson wrote:
The braces are not necessary here.
Are braced incorrect here? I've been putting braces around all variable names, so that there are fewer syntax exceptions for me to remember... - --Bob. On 2017-01-09 05:47 PM, Chris F.A. Johnson via talk wrote:
On Sun, 8 Jan 2017, Mauro Souza via talk wrote:
Or nFname="${base}.mp4"
Using brackets you can mix variables and text and create something like this: newName="${oldname}-bkp-${filedate}.${extension}.old"
The braces are not necessary here. This will work fine:
newName="$oldname-bkp-$filedate.$extension.old"
Braces would be necessary if you were using underscores instead of hyphens because they can be part of a variable name:
newName="${oldname}_bkp_$filedate.$extension.old"
- -- - -- Bob Jonkman <bjonkman@sobac.com> Phone: +1-519-635-9413 SOBAC Microcomputer Services http://sobac.com/sobac/ Software --- Office & Business Automation --- Consulting GnuPG Fngrprnt:04F7 742B 8F54 C40A E115 26C2 B912 89B0 D2CC E5EA -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Ensure confidentiality, authenticity, non-repudiability iEYEARECAAYFAlh0I9wACgkQuRKJsNLM5erWdQCgohmGdSVT2s1DHEWs+3+I3BCn nR4AoM8TVyt53lC3KhHC6uecvy09Zq9z =Af5d -----END PGP SIGNATURE----- --- Talk Mailing List talk@gtalug.org https://gtalug.org/mailman/listinfo/talk

On Mon, 9 Jan 2017, Bob Jonkman via talk wrote:
Chris F.A. Johnson wrote:
newName="${oldname}-bkp-${filedate}.${extension}.old"
The braces are not necessary here.
Are braced incorrect here? I've been putting braces around all variable names, so that there are fewer syntax exceptions for me to remember...
They are not incorrect, just unnecessary. The only time they are necessary is when the variable name is followed by a character that is legal in a variable name. Or when modifying the output of the variable expansion. -- Chris F.A. Johnson, <http://cfajohnson.com>
participants (6)
-
Bob Jonkman
-
Chris F.A. Johnson
-
Mauro Souza
-
Michael Galea
-
Stephen
-
William Park