Quick Linux Shell Script Example

November 22nd, 2007 by rvdavid Leave a reply »

When I first moved to linux I had become addicted to keeping my distro updated and so I found myself typing and retyping

$ sudo aptitude update
$ sudo aptitude dist-upgrade

At least once a day. Having read about shell scripts, I thought I’d try my hand at putting this into a shell script which, if you don’t already know, is linux’ more powerful answer to the Windows batch file.

So that’s what I did, and here’s how I did it.

Get organised – I created a shell-scripts directory in my home directory. All my other shell scripts will reside in this directory should I find another need for a shell script. -> mkdir ~/shell-scripts [enter]

Create the empty text file – I created an empty file and called it update-xubuntu.sh -> vim ~/shell-scripts/update-xubuntu.sh [enter]

Start scripting! – Every shell script needs to start with a specific declaration in the first line of where to find the executable to run the commands you are going to type in below it – the generic for ubuntu is “/bin/sh “-> #!/bin/sh [... new line and continue script]

Make it executable – After I finished my script, I had to make it executable so that it can be run straight from command line without needing to feed it as a parameter to the sh program. -> chmod a+x ~/shell-scripts/update-xubuntu.sh (alternatively, I could have done chmod 0755 ~/shell-scripts/update-xubuntu.sh – basically, what a+x means that the everyone (all) should have the permission to eXecute the file).

Run it! – You’d think it’s straight forward, but to run my newly made executable shell script in the directory I’m currently in, I will need to type in the path of the file eg if I am inside the shell-scripts directory, I will need to execute the shell script by preceding the filename with a dot slash (./) this actually tells the shell that the current directory is where the shell script is and helps the shell resolve the location of your script. -> ./update-xubuntu.sh [enter]

Gosh, hope that made sense, whatever, just keep in mind dot slash (./) means “current directory” and if you are running a shell script, you need to include it! (unless of course you add your directory to your PATH environment variable…. but that’s outside the scope of this commentary).

The Shell Script: update-xubuntu.sh

#!/bin/sh # shell scripts need to start with this line. I'm sick of trying to explain this, so I won't.
sudo aptitude update # use aptitude to update your sources
sudo aptitude dist-upgrade # run a dist-upgrade.

The Terminal

rvdavid@desktop:~$ mkdir shell-scripts # get organised
rvdavid@desktop:~$ vim shell-scripts/update-xubuntu.sh # create empty text file + Start Scripting
rvdavid@desktop:~$ chmod a+x shell-scripts/update-xubuntu.sh # Make it executable
rvdavid@desktop:~$ cd shell-scripts/ # I didn't really need to do this, but I thought I'd do it just to make an example of dot slash in the next line.
rvdavid@desktop:~/shell-scripts$ ./update-xubuntu.sh
[sudo] password for rvdavid:
Get:1 http://archive.canonical.com gutsy Release.gpg [191B]
Ign http://archive.canonical.com gutsy/partner Translation-en_AU
Get:2 http://archive.ubuntu.com gutsy Release.gpg [191B]
Ign http://archive.ubuntu.com gutsy/main Translation-en_AU
Hit http://archive.canonical.com gutsy Release
Ign http://archive.ubuntu.com gutsy/restricted Translation-en_AU
Ign http://archive.ubuntu.com gutsy/universe Translation-en_AU
Ign http://archive.ubuntu.com gutsy/multiverse Translation-en_AU
if you enjoyed this post, make sure you subscribe to my RSS feed!
You can also follow me on Twitter here.

Related posts:

  1. Trying out the Fedora 7 Linux Distro
  2. Your “Legacy Code” – Quick HIDE!
  3. Working in a Linux environment: A month on.

1 comment

  1. David says:

    Thanks, this is exactly what I needed.

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.