You are not logged in.

#1 2023-06-09 16:34:28

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 272
Website

SOLVED Script error

I am trying to write a simple bash script out to disk from another script and it errors. The code in question works fine so that is not the issue. Here is what I have from my testing:

#!/usr/bin/env bash

# error handling
set -Euo pipefail

# stubs
print() { tput setaf 2 ; echo -e "\n\t$1\n" ; tput sgr0 ; }
alert() { tput setaf 1 ; echo -e "\n\t$1\n" ; tput sgr0 ; }
trap '{ alert "${0##*/} line:$LINENO -> $BASH_COMMAND" ; exit ; }' err

# verify userid
[[ "$(id -u)" != 0 ]] && { print "Run this script with sudo" ; exit ; }

## -- MAIN -- ##

# avahi hook
cat > /etc/pacman.d/hooks/avahi.hook << _eof_
[Trigger]
Type=Package
Operation=Install
Operation=Upgrade
Target=avahi
[Action]
Description=Running Avahi Hook
When=PostTransaction
Exec=/etc/pacman.d/hooks.bin/avahi.sh
_eof_

# avahi hook script
cat > /etc/pacman.d/hooks.bin/avahi.sh << _eof_
#!/usr/bin/env bash
# hide unused avahi apps
for app in avahi-discover bssh bvnc ; do
	if [[ ! $(awk '/Hidden=true/' /usr/share/applications/"$app".desktop) ]] ; then
		echo 'Hidden=true' >> /usr/share/applications/"$app".desktop
	fi
done
_eof_
chmod +x /etc/pacman.d/hooks.bin/avahi.sh

Shellcheck gives me this error:

In test.sh line 34:
	if [[ ! $(awk '/Hidden=true/' /usr/share/applications/"$app".desktop) ]] ; then
                                                               ^--^ SC2154 (warning): app is referenced but not assigned.

For more information:
  https://www.shellcheck.net/wiki/SC2154 -- app is referenced but not assigned.

I can make shellcheck happy by changing app to A but I get an unbound variable error when I try to run it.

My first guess was cat & sudo so I tried tee and that gave me the same error.

My next guess was that if is running in a subshell but I don't know what to do with that.

Not sure what to try next.

Last edited by lenhuppe (2023-06-09 23:22:19)


Why do we drive on the parkway and then park in the driveway?

Offline

#2 2023-06-09 17:04:18

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

Re: SOLVED Script error

lenhuppe wrote:

... and it errors.

What does this mean?  Post the error.

lenhuppe wrote:

The code in question works fine so that is not the issue.

Huh?  Do you get errors, or does it work fine? (edit: see the next post for the actual error explanation).

But you could also just get rid of the if, awk, and entire loop along with the variable:

cat > /etc/pacman.d/hooks.bin/avahi.sh << "_eof_"
#!/usr/bin/env bash
# hide unused avahi apps
sed -i '/^Hidden=/d;$aHidden=true' /usr/share/applications/{avahi-discover,bssh,bvnc}
_eof_

With an added benefit, this will remove, rather than just override any existing "Hidden=false" or other Hidden settings.  Though this does make the same assumption as your approach that the Hidden value belongs in the last section of the file. (edit: this would also require a quoted _eof_, or escaping of the $ in the sed script ... I'd advise the former).

Last edited by Trilby (2023-06-09 17:25:08)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2023-06-09 17:19:38

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: SOLVED Script error

You need to either backslash-escape the dollar signs in that here document or quote the first "_eof_" to prevent premature expansion of variables etc.

https://man.archlinux.org/man/bash.1#Here_Documents

Offline

#4 2023-06-09 23:21:48

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 272
Website

Re: SOLVED Script error

Double quoting the _eof_ per Raynman's suggestion did the trick. I will need to read up on that.

I also like Trilby's suggestion with regards to using sed. I will look closer at that too.

Thank you both.


Why do we drive on the parkway and then park in the driveway?

Offline

#5 2023-06-09 23:27:19

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 272
Website

Re: SOLVED Script error

Trilby wrote:
lenhuppe wrote:

... and it errors.

What does this mean?  Post the error.

lenhuppe wrote:

The code in question works fine so that is not the issue.

Huh?  Do you get errors, or does it work fine? (edit: see the next post for the actual error explanation).

But you could also just get rid of the if, awk, and entire loop along with the variable:

cat > /etc/pacman.d/hooks.bin/avahi.sh << "_eof_"
#!/usr/bin/env bash
# hide unused avahi apps
sed -i '/^Hidden=/d;$aHidden=true' /usr/share/applications/{avahi-discover,bssh,bvnc}
_eof_

With an added benefit, this will remove, rather than just override any existing "Hidden=false" or other Hidden settings.  Though this does make the same assumption as your approach that the Hidden value belongs in the last section of the file. (edit: this would also require a quoted _eof_, or escaping of the $ in the sed script ... I'd advise the former).

I thought that I was being clear when I said that the error is an unbound variable. ( I am autistic and don't always communicate well. )

Your suggestion looks like sound advice and I plan to try it.


Why do we drive on the parkway and then park in the driveway?

Offline

Board footer

Powered by FluxBB