[SOLVED] Bash script delete (rm) permission denied

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
malonn
Level 2
Level 2
Posts: 73
Joined: Mon Sep 27, 2021 9:22 am
Location: Ohio

[SOLVED] Bash script delete (rm) permission denied

Post by malonn »

Hi all. I wrote the following bash script

Code: Select all

#!/bin/bash
if [[ ! $(sudo echo 0) ]]; then
	exit 1
	fi
# Directory names subject to change
echo Deleting Python files...
rm ~/.local/bin/*
rm -r ~/.local/lib/python3.10
rm -r /usr/local/lib/pkgconfig
rm -r /usr/local/lib/python3.10
rm /usr/local/lib/libpython3.10.a
rm cd /usr/local/bin/*
rm /usr/local/man
rm -r /usr/local/share/man
It is set as executable via the OS GUI. When I run it, the folders in the root directory do not get removed. It throws a permission denied error.
How can I get the script to remove those folders? The folders and files are removed just fine if I run directly via the terminal. What gives? Thanks.
Last edited by LockBot on Sun Jan 01, 2023 11:00 pm, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
karlchen
Level 23
Level 23
Posts: 18238
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Bash script delete (rm) permission denied

Post by karlchen »

Hello, malonn.

The main question is:
What are you trying to achieve with this script?
Why do you not uninstall cleanly something, which you have installed previously following some installation instruction. Such instructions frequently also hold uninstallation instructions.

In case you assume that the initial if block makes sure that the script is run with root privileges: No, it does not.
As a consequence, all rm commands will very likely be run under your user account.
This would then explain why some fail due to missing permissions.

Code: Select all

rm /usr/local/lib/libpython3.10.a
rm cd /usr/local/bin/* ### <== what is the "cd" command doing in here?
rm /usr/local/man
rm -r /usr/local/share/man
And I seriously wonder whether it may not be a good thing that the rm commands fail.

Regards,
Karl
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 792 days now.
Lifeline
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script delete (rm) permission denied

Post by rene »

As karlchen said, that initial if is wrong; it's testing output of sudo echo 0 and that output will always be not nul. Its attempted test is moreover superfluous: if you start the script from the menu as your user then your user is who runs it.

If you really want to do as you do start the script from the menus as e.g. pkexec /home/<user>/bin/script.sh (and without that initial if); this will request your admin password and run it elevated. However, it's a bit silly anyway: I'd suggest to just run e.g. sudo script.sh from the command-line for something like this.

On the command line the permission issues would by the way be no other -- and there's a rm cd and an rm /usr/local/man without an -r in there -- so no way that this runs "just fine" from a command line either, but in any case then, what you want is to start the script elevated through pkexec in a graphical context or plain old sudo in a command line one.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Bash script delete (rm) permission denied

Post by AndyMH »

If you want to check if running as root:

Code: Select all

#check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo "script needs to be run with sudo."
    exit
fi
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script delete (rm) permission denied

Post by rene »

... and as I seem by and large unable to not point out in this context I tend to start scripts needing root with

Code: Select all

[ $(id -u) -eq 0 ] || exec sudo "$0" "$@"
which has them relaunch themselves via sudo if needed.

But note; this is here not relevant: sudo needs a terminal for one. From the menu's you'd like that pkexec.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Bash script delete (rm) permission denied

Post by AndyMH »

Agreed, GUI use pkexec. If running from a GUI I'd want some visual indication of what it was doing, so either adding notify-send statements to the script or using zenity/yad for some popup windows.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
malonn
Level 2
Level 2
Posts: 73
Joined: Mon Sep 27, 2021 9:22 am
Location: Ohio

Re: Bash script delete (rm) permission denied

Post by malonn »

Okay, thanks all. Some stuff to chew on here.
@karlchen, I wrote the script to remove old Python installation files. There is no installer/uninstaller. I build the files and install with "make altinstall"

@rene, I just wanted to double-click it and run it. Looks like your posts regarding pkexec are what I may need. I didn't really want to launch it from the menu. Also, I posted an old version of the script by accident. The working version cleans up any typos, etc.

@AndyMH, thanks. I still have a lot to learn about Linux and the terminal.

All things considered, what I want to do is set the script as executable, double-click it and run as admin. I may need pkexec for that, but I may just call it from the terminal as admin. I'll play around with it/search engine things some. You guys/gals put me on the right track.
rigson23
Level 1
Level 1
Posts: 2
Joined: Thu May 05, 2022 4:15 am

Re: [SOLVED] Bash script delete (rm) permission denied

Post by rigson23 »

malonn,

I see your thread is noted as solved. May I just add a thought that you may or may not find helpful.

If your device is a home computer and given your desire to "double-click it and run as admin", have you considered passing the sudo password from a hidden password file (say .pswd) into your script via cat as in -

Code: Select all

cat .pswd | sudo -S rm ~/.local/bin/* && sudo rm -r ~/.local/lib/python3.10 etc
I use this for a couple of my own home maintenance scripts run via cron.

A less secure method would be putting the password into the script itself via echo as in -

Code: Select all

echo 123456 | sudo -S rm ~/.local/bin/* && sudo rm -r ~/.local/lib/python3.10 etc
where 123456 is your password for sudo.

So, in both cases, double-click and run with elevated privileges without user input.
User avatar
malonn
Level 2
Level 2
Posts: 73
Joined: Mon Sep 27, 2021 9:22 am
Location: Ohio

Re: [SOLVED] Bash script delete (rm) permission denied

Post by malonn »

No, I never thought of that. I'm a bash script/terminal noob, and haven't done much work with it.

Thanks for the tip.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: [SOLVED] Bash script delete (rm) permission denied

Post by AndyMH »

have you considered passing the sudo password from a hidden password file
The better/modern way is to use polkits. Define a policy for your script that allows it to run without a password and run the script with pkexec.
Some basics here:
viewtopic.php?f=47&t=317804&p=1800030&h ... d#p1800030
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
Locked

Return to “Scripts & Bash”