You are not logged in.
Pages: 1
Hello Everyone,
I was attempting to make a simple pacman wrapper named pkg, code below:
#!/bin/bash
if [ $1 = "install" ]; then
sudo pacman -S $2
fi
if [ $1 = "update-all" ]; then
sudo pacman -Syyu
fi
if [ $1 = "update" ]; then
sudo pacman -Syyu $2
fi
if [ $1 = "sync" ]; then
sudo pacman -Syy
fi
if [ $1 = "search" ]; then
sudo pacman -Ss $2
if [ $1 = "install-from-file" ]; then
sudo pacman -U $2
fiHowever, when I run it, I get an error:
$ sh pkg update
:: Synchronizing package databases...
testing 21.8 KiB 107K/s 00:00 [###################################################################################] 100%
core 130.0 KiB 333K/s 00:00 [###################################################################################] 100%
extra 1645.4 KiB 853K/s 00:02 [###################################################################################] 100%
community 4.4 MiB 1436K/s 00:03 [###################################################################################] 100%
multilib 170.1 KiB 5.54M/s 00:00 [###################################################################################] 100%
:: Starting full system upgrade...
there is nothing to do
pkg: line 20: syntax error: unexpected end of fileI tried making a copy, pkg2, executable with chmod +x, but it returned the same error.
What is wrong with this script?
Last edited by SealsRock12 (2018-06-16 23:28:55)
-- SealsRock12
Offline
Did you count how many 'if's and how many 'fi's the script has?
Edit:
See also http://tldp.org/LDP/Bash-Beginners-Guid … 07_03.html
Last edited by loqs (2018-06-16 23:24:12)
Offline
Oh, thank you @loqs. I had 1 too little 'fi's. That fixed it.
Marking as solved.
-- SealsRock12
Offline
One other thing, which is probably not related to the error, is that you use #!/bin/bash as the shebang, but then in the shell you call it with sh.
Offline
Many, many things.
First, if you are using bash, use `[[` instead of `[` See: https://mywiki.wooledge.org/BashFAQ/031
Next, you want a case statement, not all those if...thens: https://mywiki.wooledge.org/BashGuide/T … _select.29
Quote your variables and read up on arrays: you won't always be passing a single argument to the script: https://mywiki.wooledge.org/BashGuide/Arrays
Learn how to indent; it will make your scripts readable: https://mywiki.wooledge.org/BashGuide/P … eadability
Finally, use set -x to debug: https://mywiki.wooledge.org/BashGuide/P … Debug_Mode
Offline
And get rid of that "sync" command and double 'y' flags:
#!/bin/bash
cmd=$1
shift
case $cmd in
install) sudo pacman -S $@ ;;
update) sudo pacman -Syu $@ ;;
# sync) sudo pacman -Syy ;; # Just don't do this ... ever
search) pacman -Ss $@ ;; # No need for sudo here
install-from-file) sudo pacman -U $@
esac"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Pages: 1