Useful Bash Scripts?

Get your foo on.

Useful Bash Scripts?

Postby Bukimari » June 12th, 2015, 6:27 am

So I recently got into some basic bash scripting and now I find myself loving the fact that the terminal can do things I never thought it could. I guess what this post is about is are there any bash scripts that you use that simplify certain tasks or just do cool things? The coolest thing I think I have is a script I wrote that autostarts when I login that displays Screenfetch and then asks if I would like to update the system. I can choose 'y' or 'n' but if I hit some weird key for some reason it will just loop until I hit the right thing. Note: I'm new to this scripting stuff (I just Googled how to do most of it and used basic trial and error processes), so if I've made any mistakes or there's a simpler way of doing something please let me know.

screenfetch-script
Code: Select all
#!/bin/bash

echo
screenfetch

update=1
echo -n "Welcome back `whoami`, would you like to update the system? (Y,n)"
while [ $update = 1 ]; do

read update
if [[ $update == "y" || $update == "Y" ]]
then
   update
else
   if [[ $update == "n" || $update == "N" ]]
   then
      read -p "Update bypassed, press any key to exit..." -n1 junk
   else
      echo -n "Command not recognized, update all systems? (Y,n)"
      update=1
   fi
fi
done


update (referred to in line 13 of screenfetch-script)
Code: Select all
#! /bin/bash

sudo apt-get update && sudo apt-get upgrade
dist=1
echo -n "Perform dist-upgrade as well?(y,N)"
while [ $dist = 1 ]; do

read dist
if [[ $dist == "y" || $dist == "Y" ]]
then
   sudo apt-get dist-upgrade
   read -p "Update completed, press any key to exit..." -n1 junk
else
   if [[ $dist == "n" || $dist == "N" ]]
   then
      read -p "Update completed, press any key to exit..." -n1 junk
   else
      echo -n "Command not recognized, perform dist-upgrade? (y,N)"
      dist=1
   fi
fi
done


I was going to upload a picture of the screenfetch-script working but I can't figure out how to attach a picture from my computer using the img BBCode thing. Oh well, let me see some cool scripts!
"Hey." "Yeah?" "Do you ever wonder why we're here?" "No, I never wonder why we're here. Semper Fi, ******."
Bukimari
 
Posts: 6
Joined: August 17th, 2014, 9:43 pm

Re: Useful Bash Scripts?

Postby fsmithred » June 13th, 2015, 12:00 pm

Here's a slightly different way to do what you're doing. I changed the structure of the main question, and I turned your second script into a function, so you only have to deal with one file. I didn't test that, so it possible that it needs to be modified. I also replaced whoami, an external command, with an environment variable. Your script may run a few nanoseconds faster that way.

Another thing that might be worth changing is to run the upgrade with the -s option first, so you can see what packages will be upgraded, and then ask to proceed or not. Then do the same with dist-upgrade. Also, you probably don't need to ask the user to press a key to exit when the script completes. Just echo "Update completed" and exit.

Code: Select all
#!/usr/bin/env bash

run_update () {
sudo apt-get update && sudo apt-get upgrade
dist=1
echo -n "Perform dist-upgrade as well?(y,N)"
while [ $dist = 1 ]; do

read dist
if [[ $dist == "y" || $dist == "Y" ]]
then
   sudo apt-get dist-upgrade
   read -p "Update completed, press any key to exit..." -n1 junk
else
   if [[ $dist == "n" || $dist == "N" ]]
   then
      read -p "Update completed, press any key to exit..." -n1 junk
   else
      echo -n "Command not recognized, perform dist-upgrade? (y,N)"
      dist=1
   fi
fi
done
}


while true ; do
   echo -n "Welcome back ${USER}, would you like to update the system? (Y,n)"
   read update
   
   case $update in
      [Yy]) run_update ;;
      [Nn]) exit 0 ;;
      *) echo "Remove this line if you want the script to keep asking until you give a valid answer (Y, y, N or n)" ; exit 0 ;;
   esac
done


Here are some scripts I wrote awhile ago:
https://github.com/fsmithred
fsmithred
 
Posts: 341
Joined: February 11th, 2011, 4:14 am

Re: Useful Bash Scripts?

Postby Bukimari » June 14th, 2015, 1:08 am

Cool, so the first half of that is the update script being turned into a function and then it gets called if you choose yes in the main script? I figured there was an easier way to do it other than a bunch of nested if-then's but on the upside it gave me a simple command to run by itself to just update my system by typing "update" as opposed to "sudo apt-get update", etc. I would have it just exit without user input but with it launching on start, when it exits it also exits the terminal so I won't see the results if it does. Definitely going to do the -s though, I was wondering why it would just go straight through. Will test this script tonight sometime, thanks!
"Hey." "Yeah?" "Do you ever wonder why we're here?" "No, I never wonder why we're here. Semper Fi, ******."
Bukimari
 
Posts: 6
Joined: August 17th, 2014, 9:43 pm

Re: Useful Bash Scripts?

Postby fsmithred » June 14th, 2015, 10:44 am

Bukimari wrote:Cool, so the first half of that is the update script being turned into a function and then it gets called if you choose yes in the main script?


Yes. You could go the other way, too. If you ended up with a bunch of functions, you could move them to a separate file and then source that file in the script by adding a line (near the top) like:
Code: Select all
source /path/to/functions_file


I would have it just exit without user input but with it launching on start, when it exits it also exits the terminal so I won't see the results if it does. Definitely going to do the -s though, I was wondering why it would just go straight through. Will test this script tonight sometime, thanks!


FYI, you might like the output of 'aptitude -s ...' better than 'apt-get -s ...'. With apt-get, the condensed list of packages to be upgraded scrolls up out of view. And when you actually run the upgrade, you can prevent it from stopping to ask you if you want to proceed by adding the -y option. (automatically answers "yes" for you.)

If you're having the script run automatically on start, and you want to save the output, you could add 'tee' to the command that starts the script.
Code: Select all
/path/to/myscript | tee /path/to/myscript.log
fsmithred
 
Posts: 341
Joined: February 11th, 2011, 4:14 am

Re: Useful Bash Scripts?

Postby Bukimari » June 15th, 2015, 3:57 pm

Yes. You could go the other way, too. If you ended up with a bunch of functions, you could move them to a separate file and then source that file in the script by adding a line (near the top) like:
Code: Select all
source /path/to/functions_file


I was actually going to look into that really soon, so you saved me some Googling time, thanks! I really need to learn how to do all of this but I've just been so busy with work, gotta pay bills haha.
"Hey." "Yeah?" "Do you ever wonder why we're here?" "No, I never wonder why we're here. Semper Fi, ******."
Bukimari
 
Posts: 6
Joined: August 17th, 2014, 9:43 pm

Re: Useful Bash Scripts?

Postby Gerowen » October 5th, 2015, 6:18 am

Here's one you can tinker with, in response to your question about whether or not there's any we have that we've written to simplify things. I've got a whole folder full of scripts, but most of them are just 2-3 lines designed to run some program with commonly used arguments or to run something I have in my home folder without adding it to the PATH, etc. This is probably the biggest one I've written in Bash.

CheckMD5. I've since re-written this one in Python because the Zenity windows can be obnoxiously large and I couldn't find a way to specify a window size, plus I wanted to be able to use it in Windows, but this version still works fine. It calculates the MD5 checksum of a file and then lets you compare it to either a 2nd copy of a file (such as if you copied it off another drive and want to make sure it's alright), or compare it to a checksum you enter (such as when the site you downloaded it from gives you the checksum.

Code: Select all
#!/bin/bash
#CheckMD5 - This is a tool to compare the MD5 checksum of a given file against another file, or against a given original checksum.
############################################
#     CheckMD5 - MD5 Verification Tool     #
#            Marcus Dean Adams             #
#        marcusdean.adams@gmail.com        #
#         Updated 23 October 2013          #
############################################

#Retrieves location of file to be checked and generates an MD5 checksum.

zenity --info --text="CheckMD5 will compare the MD5 checksum of a file against an original.\n\nDepending on the size of the file(s) selected, it may take a long time to calculate the MD5 Checksum, and your computer may appear to freeze.  Please be patient.\n\nPlease select the file you wish to check." --title="CheckMD5 by Marcus Adams"

infile=$(zenity --file-selection --filename=~/Desktop --title="CheckMD5 - Select a file to check") || exit

md5=$(md5sum "$infile" | cut -b -32)

#Determines what the file should be compared to.

options=("Compare to Original File" "Compare to Entered Checksum")

opt=$(zenity --list --title="CheckMD5 - Select an Option" --text="Pick an option:" --column="Options" "${options[@]}") || exit

#Prompts the user for an original file to compare the one in question against, and generates its checksum.

if [ "$opt" == "${options[0]}" ]
then
newfile=$(zenity --file-selection --filename=~/Desktop --title="CheckMD5 - Select a file to compare to") || exit
md5correct=$(md5sum "$newfile" | cut -b -32)
original=$"of $newfile"
fi

if [ "$opt" == "${options[1]}" ]
then
md5correct=$(zenity --entry --title="CheckMD5 - Enter MD5 Checksum" --text="Enter the checksum to compare the file to:") || exit
original=$"manually entered."
fi

#Conditional statements that determine which message to display based on whether the checksums match.

if [ "$md5" == "$md5correct" ]
then
   zenity --info --title="CheckMD5 - SUCCESS!" --text="SUCCESS!\n\nThe MD5 checksum of the file you selected matches the MD5 checksum you entered.\n\nChecksum of $infile:\n\n$md5"
fi

if [ "$md5" != "$md5correct" ]
then
   zenity --info --title="CheckMD5 - Checksum Mismatch!" --text="FAILED!\n\nThe MD5 checksum of the file you selected does not match the MD5 checksum you entered.\n\nChecksum of $infile:\n\n$md5\n\nChecksum $original:\n\n$md5correct\n\nThis indicates that at some point the file in question was corrupted, and is not identical to the source file you compared it to."
fi

exit
Gerowen
 
Posts: 3
Joined: October 5th, 2015, 5:53 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 6 guests

cron

x