Can I modify this script so it makes itself be executable ?
I could use help in making my posts more visually appealing when it has code.
Thanks.
#!/bin/bash
# Release 22.04.5 LTS (Jammy Jellyfish) 64-bit
#
# Much help from https://ubuntu-mate.community/
#
# Run this periodically to clean up unneeded files
# \*\*\*\* This is in crontab.
clear
echo pain | sudo -S apt -y update
echo really | sudo apt-get dist-upgrade
echo hurts | sudo -S apt-get clean
# autoclean clears out the local repository of retrieved package files
# Local repository of package files are in /var/cache/apt/archives/
echo pain | sudo -S apt autoremove
echo CleanUp.sh was run on > /home/andy/Downloads/Trash_Cleanup$( date '+%m-%d-%Y' ).txt
date +"%I:%M:%S %P %D" >> /home/andy/Downloads/Trash_Cleanup$( date '+%m-%d-%Y' ).txt
{
echo "CleanUp.sh was run on"
date +"%I:%M:%S %P %D"
} > /home/andy/Downloads/Trash_Cleanup$( date '+%m-%d-%Y' ).txt
You can run a not executable script by invocation of bash:
/bin/bash /path/to/yourscript ...
Otherwise it must be executable, and the kernel inserts the interpreter given in the shebang.
/path/to/yourscript ...
Are you talking about how to post code in this forum?
Wrap it between ``` lines! I did it with your current post.
You can use the </> icon at the top of the editor, either on your selected code, or on a new line and then insert your code.
In addition to only running the write to your clean-up file once, I would:
(a) Assign the generated filename to a variable at the top of the script, rather than have it hidden down at the end.
(b) Append to it every time. If cron only runs once a day it makes no difference. If it runs multiple times, you probably want to see that: you might even want to test it that way.
(c) I prefer the %Y-%m-%d date format in filenames. It makes it much easier when you ls -l the directory – having all the 4th of every month sort together gets somewhat muddled. Seeing them in chronological order is easier.
Having your sudo password visible in a script can be a serious security issue.
I tend to put the exact crontab line as a comment in the script, as a reminder. crontab can occasionally get munged accidentally.
I want to make sure I have a backup done at least once a day.
#!/bin/sh
# Backup files to maxtor drive before the computer has been suspended
# Put this in /lib/systemd/system-sleep/
case "$1" in
pre)
logger Files being backed up to Maxtor drive.
/home/andy/bin/Backup_Ubuntu_Mate.sh
logger done with backup, going to sleep.
;;
post)
logger waking up from sleep.
;;
esac
Yes, but only if it is run by a user with permission to do so. How:
At the start or end of the script, add:
chmod +x "$0"
This will make the script itself executable if it is not already.
2. How to post visually appealing code on forums
Use Markdown code blocks:
For most forums (including Stack Exchange, GitHub, Discourse, and many Linux/tech forums), wrap your code in triple backticks (```) before and after the code, and optionally specify the language for syntax highlighting.
Example:
```bash
#!/bin/bash
# ...rest of your script...
chmod +x "$0"
```
For Ubuntu-Mate community:
It supports Markdown-style code blocks.
Use triple backticks as above for blocks.
Use single backticks for inline code.
Add brief comments/section headers to explain logic.
Avoid excessive indentation.
Preview your post before publishing.
3. Your script, improved formatting & with self-executable line:
#!/bin/bash
# Release 22.04.5 LTS (Jammy Jellyfish) 64-bit
# Much help from https://ubuntu-mate.community/
# Run this periodically to clean up unneeded files
# This is in crontab.
chmod +x "$0" # Makes script executable
clear
echo pain | sudo -S apt -y update
echo really | sudo apt-get dist-upgrade
echo hurts | sudo -S apt-get clean
# Autoclean clears out local repository of retrieved package files
echo pain | sudo -S apt autoremove
# Log file output
echo "CleanUp.sh was run on" > "/home/andy/Downloads/Trash_Cleanup$(date '+%m-%d-%Y').txt"
date +"%I:%M:%S %P %D" >> "/home/andy/Downloads/Trash_Cleanup$(date '+%m-%d-%Y').txt"
Summary:
Add chmod +x "$0" to make script executable.
Use triple backticks and specify language (e.g., bash) for best code formatting in posts.
1. How do you execute a script that is not executable?
Call the interpreter explicitly:
bash myscript.sh
or
sh myscript.sh
This runs the script regardless of file executable bit.
2. Once a script is executable, does it make sense for it to make itself executable?
No.
If you're already running the script, the file is either already executable, or you’re calling it directly via the interpreter (see above).
chmod +x "$0" is redundant during execution—it won’t help anything in the running session.
Only useful if you want to guarantee the script stays executable for future runs or for other users, but generally, you make it executable once at install/setup, not every run.
Summary Table
Not Executable
Executable
bash script.sh
./script.sh
sh script.sh
Bottom line:
Execute non-executable scripts with bash script.sh.
No real point in making a script make itself executable every run.
Your script comments that it is executed via crontab. I have not verified any of this, but will edit this post later to confirm these notes. I am referring to man -s 5 crontab and command 'cron' from deb cron (3.0pl1-137ubuntu3)
(1) “Each command is executed by /bin/sh, or by the shell specified in the SHELL variable of the crontab file.”
On my Linux Mint 21.3, I get lrwxrwxrwx 1 root root 4 Nov 16 2024 /bin/sh -> dash, which may not be what you expected.
(2) If your cron command is like /bin/bash -c ‘myScriptName’, cron will run /bin/sh, which will run /bin/bash, which (I suspect) will ignore the shebang in your script (as it is already in bash) and not require the file to be executable. So setting execute status at all may not be necessary, let alone every time it runs.
(3) It is fairly unclear what “executed by” means here. cron puts a wrapper round your command, which changes to your own user, puts you in your own home directory, and collects any output to stdout and stderr so it can (by default) mail it to you. Whether that uses your shebang is unclear.
The flaw seems to be that the original script needs to be executable so that it can run, and thus create the other script. Something of a Catch-22. If it could run, it could change its own permissions.
It can however shoot itself in the foot by removing its own execute permissions. This might be useful, to make a run-once script.
paul: ~ $ cat myFoot
#! /bin/bash
set -x
ls -l "$0"
chmod -x "$0"
ls -l "$0"
chmod +x "$0"
ls -l "$0"
chmod -x "$0"
ls -l "$0"
echo Ran myFoot
paul: ~ $ ls -l ./myFoot
-rw-rw-r-- 1 paul paul 124 Sep 22 09:25 ./myFoot
paul: ~ $ chmod +x ./myFoot
paul: ~ $ ls -l ./myFoot
-rwxrwxr-x 1 paul paul 124 Sep 22 09:25 ./myFoot
paul: ~ $ ./myFoot
+ ls -l ./myFoot
-rwxrwxr-x 1 paul paul 124 Sep 22 09:25 ./myFoot
+ chmod -x ./myFoot
+ ls -l ./myFoot
-rw-rw-r-- 1 paul paul 124 Sep 22 09:25 ./myFoot
+ chmod +x ./myFoot
+ ls -l ./myFoot
-rwxrwxr-x 1 paul paul 124 Sep 22 09:25 ./myFoot
+ chmod -x ./myFoot
+ ls -l ./myFoot
-rw-rw-r-- 1 paul paul 124 Sep 22 09:25 ./myFoot
+ echo Ran myFoot
Ran myFoot
paul: ~ $ ./myFoot
bash: ./myFoot: Permission denied
paul: ~ $
I did make the script, and ran it, in my previous post.
The "$0" throughout the topic is a bit of a joke. Scripts can be passed arguments -- extra parameters like options and filenames.
The system passes the script its own name first, so "$0" would literally be passed as "./myFoot". You might think a script knows what it is called, but actually there are some occasions where the same script can usefully be called with different names.
rm "$0" would make any script destroy itself (assuming it had permissions that allowed that to happen).
My command line prompt is paul: ~ $ so that precedes each command I type. The script is called myFoot because it does shoot itself in the foot.
So cat myFoot lists the script, starting with the shebang and ending with an echo command.
The next three commands show the file permissions (with ls), make it executable, and show that it became executable.
I then run the script. This test directory is not on my PATH, so I specify my current directory like
./myFoot.
The script includes set -x, which makes it print every script command before it does it, marking it with a + in the first column.
The commands show that the script can set the execute permissions off and on successfully. I carefully leave it off at the end.
Finally, I run the script again from the command line, and it fails because it made itself non-executable.
Quite so. But that snippet does not explain why it works, why it might have an unexpected side effect, or why it might fail if you were not using a GTK-based desktop.
I had a very fortunate 50-year career, 1968 to 2018. The last 30 years were as a consultant (very hands-on) in electricity supply systems, including 7 years at UK National Grid. The main vendors for their system were the same guys who blacked out 55 million consumers in the USA in 2003 (culminating in blacking out their own control room). You would be surprised how much stuff needs pumping: water, sewage, aircon, gas and diesel; and things like freezers (domestic, shop and warehouse), cell phone masts, elevators, hospital equipment, subway trains, and EV charging.
So imperfection could mean letting the system energise a 275,000 volt cable that some poor rigger was working on at the top of a transmission tower, or dropping the entire power supply to Heathrow Airport for two days (which actually happened a few months ago). Not on my watch, thank you!