0

Writing my first bash function, trying to execute it from shell, the contents of the file look like this:

#!/bin/bash
myFirstFunction(){
curl --url "$1" --output "$2" #;
ffmpeg --i "$2" -c:v "$3"; #$2 becomes the input and $3 becomes the new output.
rm "$2" #then I clean up by deleting $2.
}

#end

I give the script permission to run without my typing bash:

chmod +x myfirstscript.sh

And from shell I tried to fun the function like this:

myfirstscript.sh myFirstFunction inputFN firstOutputFN lastOutputFN

My question:

As far as manuals and tutorials, I cannot find out how to call the function out of the file and pass all the arguments to the function. Can you please show me how?

Carriage Returns Update:

I had been getting this error: myfirstfunction.sh: line 2: syntax error near unexpected token $'{\r''`. I fixed this by replacing \r\n with \n via notepad++.

Any guidance would be greatly appreciated.

Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
  • 1
    Your file probably has Windows-style CRLF line endings and the shell that's trying to execute it is expecting Unix-style LF line endings. – Shawn Nov 08 '22 at 06:57
  • 1
    Install `dos2unix` in your WSL environment and run it on the file. – Shawn Nov 08 '22 at 06:57
  • 1
    `$'\r'` is the bash denotation of a string consisting of a sole CR. – user1934428 Nov 08 '22 at 07:01
  • Yes, this rids the error, but there is still the main question "how do I call the function from shell". I don't know how to call the function from the debian terminal. I can't find documentation on how to call the function from the file with arguments. – Wolfpack'08 Nov 08 '22 at 07:37
  • If you run the script normally, it runs as a subprocess, and the function is only defined in that subprocess (and vanishes when the script finishes and the subprocess exits). You can use `source` or `.` to run the script in your shell (rather than a subprocess), and then just run `myFirstFunction firstarg secondarg etc`. – Gordon Davisson Nov 08 '22 at 07:52
  • @GordonDavisson Yes. I did use `source scriptname.sh; functionName firstarg secondarg thirdarg` – Wolfpack'08 Nov 08 '22 at 15:02

0 Answers0