Bash script make itself executable

Is there a way to make this make itself executable?

Thanks. :slight_smile:

cat > somescript.sh << \EOF
#!/bin/bash
block_count=$(sudo tune2fs -l /dev/sda1 | awk '/^Block count:/ {print $NF}')
reserved_block_count=$(sudo tune2fs -l /dev/sda1 | awk '/^Reserved block count:/ {print $NF}')

perl -e 'printf("%.1lf%%\n", ($ARGV[0] * 100.0 ) / $ARGV[1])' "$reserved_block_count" "$block_count"
EOF

I am not quite sure what you mean by "this": you show a script (without a specified name) producing another script (named "somescript.sh").

If you want the original noname-script to be executable, then put its text into a normal file (i.e. /some/where/script.sh ) and issue this command:

chmod 754 /some/where/script.sh

This will make it executable for the owner (7) and the owners group (5), which most times makes sense. If you have special security considerations this might have to be changed.

If you just want the created script ( somescript.sh ) to be executable then add this to your script:

cat > somescript.sh << \EOF
#!/bin/bash
block_count=$(sudo tune2fs -l /dev/sda1 | awk '/^Block count:/ {print $NF}')
reserved_block_count=$(sudo tune2fs -l /dev/sda1 | awk '/^Reserved block count:/ {print $NF}')

perl -e 'printf("%.1lf%%\n", ($ARGV[0] * 100.0 ) / $ARGV[1])' "$reserved_block_count" "$block_count"
EOF
chmod 700 somescript.sh

although i suggest that you use absolute pathes in scripts. This will create the script anywhere - and likely not where you expect it to be.

I hope this helps.

bakunin

Thanks.

Using chmod +x somescript.sh lets me run script without inputting pw.

I place all my scripts in /bin so they can be run from any dir.

You really should not use /bin - a link usually to /usr/bin - for anything.

Bakunin (some others ) posted a long dicsussion of how this can make trouble for you. I cannot find the link at the moment, perhaps he can find it for you.

1 Like

My post was a little incomplete.

/bin was short for /home/andy/bin .

I know not to put scripts in root owned directories.

This is what I ended up with.
gxmessage is very handy.

#!/bin/bash
# Ubuntu_Mate 16.04 LTS
#
# Show reserved blocks
#
echo marlin | sudo -S tune2fs -l /dev/sda1 > list_tune.txt
# looks for block count and IS case insensitive
#
echo "Reserved Blocks (Reserved block count should be 1% of Block count)" > list_reserved.txt
grep -i "block count" list_tune.txt >> list_reserved.txt

# Show block and reserved block counts
gxmessage -fg red -font  'sans 20' -timeout 3 -file list_reserved.txt