Syntax error: "(" unexpected (expecting "fi")
linux, shell
Solution
Here is your script, rewritten to work.
#!/bin/bash
if [ x$1 = x-l ] ; then
BASE=/lib/modules/$(uname -r)/
if [ x$2 = x ] ; then
find $BASE -name '*.ko' | sed -e "s#$BASE##g"
else
find $BASE -name '*.ko' | sed -e "s#$BASE##g" | grep $2
fi
else
/sbin/modprobe $@
fi
Your main problem was a ton of backslashes that broke the script. If you are using `$(command)` to get the output of a command, you need that dollar sign to be left alone; `\$(command)` will turn into a literal string `"$(command)"` which isn't what you wanted. Likewise, your `sed` command lines are using the `#` character to delimit the search and replace strings, so you don't need to put a backslash before the slashes in the paths. Likewise, you need to just have `$2` for the second argument; `\$2` is the literal string `"$2"`.
I used a common convention for testing the `$1` and `$2` arguments: I put an `x` before them in the test. This works even in old wacky UNIX shells. I guess with a GNU Bash on Linux you can count on the `[` operator being built-in, but doing it this way still works.
Also note how I indented the `if`/`else` statements. This is purely a style thing and you don't have to do things the way I do, but I find this to be the most readable.
Finally, this script is approaching the level of complexity where I would consider rewriting it in a more powerful language than Bash. I personally prefer Python but you could use Ruby or whatever you prefer.
Problem
Error messages: ``` Syntax error: "(" unexpected (expecting "fi") /sbin/modprobe.sh: 2: /sbin/modprobe.sh: EOF: not found /sbin/modprobe.sh: 10: /sbin/modprobe.sh: Syntax error: "(" unexpected (expecting "fi") ``` Shell script: ``` EOF #!/bin/bash if [[ \$1 == -l ]] then if [ -z \$2 ] then find /lib/modules/\$(uname -r) -name '*.ko' | sed -e "s#\\/lib\/modules\/\$(uname -r)\/##g" else find /lib/modules/\$(uname -r) -name '*.ko' | sed -e "s#\/lib\/modules\/\$(uname -r)\/##g" | grep \$2 fi else /sbin/modprobe \$@ fi EOF ```