You are not logged in.

#1 2018-06-16 23:17:24

SealsRock12
Member
Registered: 2018-04-03
Posts: 83

[SOLVED] What is wrong with this script?

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
fi

However, 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 file

I 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

#2 2018-06-16 23:22:26

loqs
Member
Registered: 2014-03-06
Posts: 19,113

Re: [SOLVED] What is wrong with this script?

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

#3 2018-06-16 23:28:30

SealsRock12
Member
Registered: 2018-04-03
Posts: 83

Re: [SOLVED] What is wrong with this script?

Oh, thank you @loqs. I had 1 too little 'fi's. That fixed it.


Marking as solved.


-- SealsRock12

Offline

#4 2018-06-16 23:28:58

circleface
Member
Registered: 2012-05-26
Posts: 639

Re: [SOLVED] What is wrong with this script?

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

#5 2018-06-16 23:29:28

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] What is wrong with this script?

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


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2018-06-17 00:03:36

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: [SOLVED] What is wrong with this script?

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

Board footer

Powered by FluxBB