You are not logged in.
Pages: 1
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)
"I'm suspicious of people who don't like dogs, but I trust a dog when it doesn't like a person." -- Bill Murray
Offline
... and it errors.
What does this mean? Post the error.
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
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.
Offline
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.
"I'm suspicious of people who don't like dogs, but I trust a dog when it doesn't like a person." -- Bill Murray
Offline
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.
"I'm suspicious of people who don't like dogs, but I trust a dog when it doesn't like a person." -- Bill Murray
Offline
Pages: 1