I have modified this script to download songs from YouTube but I am getting the following error when I run it like this:
sh youtube2mp3.sh https://www.youtube.com/watch?v=gpOJ6iu8fqQ
Errors:
youtube2mp3.sh: line 31: [: too many arguments
youtube2mp3.sh: line 39: [: too many arguments
youtube2mp3.sh: line 49: [: too many arguments
Sorry but the system encountered a problem.
The line numbers are referring to the three if [ -f $video_title.$ext1 ]
lines ... I thought I had the arguments ok as it worked in a previous version, but I'm stuck at this point — could someone explain what I need to do to correct it?
address=$1
video_title="$(python youtube-dl $address)"
ext1="flv"
ext2="mp4"
ext3="webm"
if [ -f $video_title.$ext1 ]
then
ffmpeg -i $video_title.$ext1 "$video_title".wav
lame "$video_title".wav "$video_title".mp3
rm $video_title.$ext1 "$video_title".wav
else
if [ -f $video_title.$ext2 ]
then
ffmpeg -i $video_title.$ext2 "$video_title".wav
lame "$video_title".wav "$video_title".mp3
rm $video_title.$ext2 "$video_title".wav
else
if [ -f $video_title.$ext3 ]
then
ffmpeg -i $video_title.$ext3 -acodec libmp3lame -aq 4 "$video_title".mp3
rm $video_title.$ext3
else
echo "Sorry but the system encountered a problem."
fi
fi
fi
Solution. So to fix the error “You've entered too many arguments for this function” you need to go through the content of the cell, character-by-character, to ensure that there are no syntax errors. In other words, you need to check whether all opened brackets are closed and all commas are properly in place.
Bash uses a tool called positional parameters to provide a means of entering data into a Bash program when it is invoked from the command line. There are ten positional parameters that run from $0 through $9, although there are ways to hack around that limit.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
You can pass more than one argument to your bash script. In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3 … The second argument will be referenced by the $2 variable, the third argument is referenced by $3 , .. etc.
Always quote the parameter expansion. The value of $video_title
is being split into multiple words, which confuses the [
command.
if [ -f "$video_title.$ext1" ]
then
ffmpeg -i "$video_title.$ext1" ...
Whenever you have a shell script you need to debug, use set -xv
. This will turn on verbose mode which will print out each line executed, and will turn on xtrace which will display the command when expansion is done.
You can turn off set -xv
with set +xv
. You can envelope your entire script, or just the lines causing your heartaches.
If you did this, I think you'll see that $video_title
gets expanded to names with spaces in them, and that's when you get your errors. You should be putting quotes everywhere in your script where you have `$video_title":
if [ -f "$video_title".$ext2 ] #QUOTES!
then
ffmpeg -i "$video_title".$ext2 "$video_title".wav #EVEN MORE QUOTES
Remember that [
is actually a command and is a synonym to the test
command. Your if
command could be written as:
if test -f "$video_title".$ext2 #QUOTES!
then
Like all commands, the shell will break up the parameters you give to the command on white spaces. Thus, your title "The Life of a Radish" will get broken up as five separate parameters "The", "Life", "of", "a", and "Radish" before being passed to this test
command.
This explains your error message:
youtube2mp3.sh: line 31: [: too many arguments
Because the -f
command line parameter can only take one additional parameter and not the five parameters the shell passed to it. The quotes keep the shell from breaking up your video title into separate parameters to the -f
flag.
By the way, print out the manpage on test ($ man test
) and you'll see that it takes all of the same parameters your [ ... ]
take. It also explains why [
and ]
need to be surrounded by spaces -- these are Unix commands and Unix commands must be surrounded by white spaces.
Also run this command:
$ ls -il /bin/[ /bin/test
10958 -rwxr-xr-x 2 root wheel 18576 May 28 22:27 /bin/[
10958 -rwxr-xr-x 2 root wheel 18576 May 28 22:27 /bin/test
That first parameter is the inode. It's sort of like the real name of the file (What you think is the file name and the directory are attributes of the inode). You will see that both test
and [
have the same inode number, and thus are really the same file that are linked (via the ln
command) to the same file.
(Not entirely true. The [
is a builtin command to both Korn and BASH which you were probably using. However, the [
builtin command is internally linked to another builtin command called test
anyway.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With