I want to run a sh file:
#!/bin/bash
for f in !(*.sh); do
ffmpeg -i "$f" -vf yadif=0:-1 -threads 0 -c:v libx264 -pix_fmt yuv420p \
-r 29.97 -b:v 3000k -s 1280x720 -preset:v slow -profile:v Main \
-level 3.1 -bf 2 -movflags faststart /mnt/media/out-mp4/"${f%.mxf}.mp4"
rm $f
done
However, I get the following error:
2: task1.sh: Syntax error: "(" unexpected
If I try directly on the command line it works perfectly.
the path and permissions are already reviewed
Any idea what might be happening?
This is not a "sh file" -- it's a bash script. If you run it with
sh yourscript
, it will not work (as extglobs, the shell feature you're trying to use, aren't supported in POSIX sh); it needs to be run only withbash yourscript
, or with./yourscript
when starting with#!/bin/bash
(as it does). Describing it as a "sh file" is thus misleading. Moreover, even with bash, the extended globbing feature needs to be turned on.Your immediate issue is that
!(*.sh)
is not regular glob syntax; it's an extglob extension, not available by default. You may have a.bashrc
or similar configuration file which enables this extension for interactive shells, but that won't apply to scripts. Run:...to enable these features.
Cleaned up, your script might look like:
Note the following changes, above and beyond formatting:
shopt -s extglob
is on its own line, before the glob is expanded.rm
is only run ifffmpeg
succeeds, because the separator between those commands is&&
, rather than either;
or a bare newline.--
argument passed torm
tells it to treat all future arguments (in this case, the content of"$f"
) as a filename, even if it starts with a dash."$f"
argument torm
is inside double quotes.You need to enable the extended globbing in the script:
Also make sure you're not running the script in a different script, e.g. by calling
sh script.sh
.