Add to Mixx!

Writing a Basic Ubuntu Script


If you have ever done a lot of repetitive tasks on a Ubuntu Linux machine, I’m sure you’ve wondered if there’s a better way of doing things. For example, say I need to run a dozen or more command line commands. I could enter them in one by one or I can put them all in a single file and run it as a script.

When you run it all as a script, you save a lot of time. Today, I am going to show you how to make the most simple Ubuntu script you can find. It doesn’t use any variables, it just helps you run several commands at once. for example, we are going to create a simple script to

First, just you need to create the script file. For this just open a terminal and type:
gedit samplescript.sh

This will open gedit with a blank file named samplescript.sh. In the file, type the following:
#!/bin/sh
echo “welcome”
firefox

Save this file. The first line identifies it as a shell script, the second line causes the script to print welcome on your screen when it’s run. After the echo, you can put in any commandline line you want to on the next line and it will run. For this example, we put the command firefox and when you run the file, it will open Firefox.

Next, run this command to make the file executable:
chmod 777 samplescript.sh

Now, to run the script, type this:
./samplescript.sh

See how easy that was? We’ll come back soon and do more complex scripts, but hopefull this can save you some time now.

Tags:


Other Related Posts

  • More Reasons Why I Love Ubuntu Linux
  • Xming with Windows XP and Ubuntu
  • RSS Content Generation
  • What Operating System Are You Using?
  • Targeted Article Writing
  • Writing is Art

  • 22 comments ↓

    #1 ben on 03.11.08 at 11:46 pm

    hey,
    thanks for the easy to follow tutorial, it helped alot. Consider I’m new to linux and still figuring it all out, this basic one was a great start :)

    one thing: check your file names — ‘upgradescript.sh’ becomes ’samplescript.sh’.

    thanks,

    ben

    #2 Louis on 03.12.08 at 7:35 pm

    Oops, thanks. I made the changes.

    #3 Dan on 03.20.08 at 12:32 pm

    I know this is old now, but I found this via google and it was quite helpful. Like the above poster (ben), I’m new to Linux, and a BASIC tutorial of scripting was very helpful.

    Thanks!

    #4 Dan on 06.02.08 at 10:50 pm

    Wow, this was helpful! Seriously, I’m using Ubuntu 8.04 and this was a big help to getting me ready to do much more complex stuff. After reading this, and another equally short tutorial, I was able to convert all my m4a files in no time, whereas before I read this, it took some great deal of time.

    #5 Veggie on 07.29.08 at 5:15 am

    I used this tort to help my wife run wow without knowing how to use the terminal. thanks!!!

    #6 David on 08.09.08 at 3:16 am

    Veggie:
    Your wife runs on Linux?????? O.o

    #7 David on 08.09.08 at 3:39 am

    I made one for a system backup but it doesn’t work x.x

    #!/bin/sh
    sudo su
    cd /
    tar cvpjf backup.tar.bz2 –exclude=/proc –exclude=/lost+found –exclude=/backup.tar.bz2 –exclude=/mnt –exclude=/sys /

    it just opens the terminal as root and sits there =(

    #8 Louis on 08.11.08 at 2:49 am

    Why did you use sudo su? Everything looks good, but I’d just take out sudo su and just use sudo tar instead. I’d also run the script as root…..

    example: sudo ./script.sh

    #9 Chiron613 on 12.09.08 at 2:01 pm

    Just a comment - it’s not an “Ubuntu” script, it’s a *shell* script. There’s a difference.

    First, Ubuntu isn’t the same thing as Linux. Ubuntu is one distribution of Linux; there are hundreds. Ubuntu is probably the most popular, and the easiest to use, but it’s not the same thing as Linux.

    Second, the commands you are describing are shell commands. They would work in many different Linux distributions, as well as in Unix and BSD and so forth. There is nothing in there that limits the scripts to Linux, much less Ubuntu Linux.

    Just my $0.02 worth…

    #10 Louis on 12.10.08 at 10:17 am

    Thanks and great note! Ubuntu is my favorite flavor of Linux and these commands work across the breadth of Linux and Unix type operating systems.

    #11 EZ Computers on 12.14.08 at 4:31 pm

    thanks for a great article :)
    this should come in handy

    #12 HellYeah on 12.26.08 at 12:47 am

    Hell yeah! Thanks!

    #13 Vicco on 01.02.09 at 3:01 pm

    Used your idea and made this simple script….it works like a charm!!!! Thanks Big Time

    #!/bin/sh
    echo “HERE WE GO LETS HOPE IT WORKS!”
    mkdir restore
    tar -zxvf site.tar.gz -C /restore
    mv /restore/var/www /var
    rm -r /restore
    echo “Yehaw…we are back up and running.”

    #14 Ruslan on 01.22.09 at 5:47 pm

    Thanks for the tutorial, it helped me a lot (I had to convert about 250 video clips using ffmpeg, but it took minutes for an operation and I had to wait to process the next one… 250 times…)
    But now it is just “cd” and “./” ;)
    Thanks again!!!

    #15 rakudave on 01.24.09 at 6:21 pm

    Be advised however that “chmod 777″ seldomly is a good idea. “chmod +x file.sh” will make it executable without giving the entire world write permission.

    #16 thanks on 02.14.09 at 7:12 pm

    So simple! I just wanted to make a really simple launcher to restart firefox. You told me how to do it in less than 2 mins.

    #17 Skynet on 02.28.09 at 11:47 pm

    Thanks, this example took the fear out of laerning scripts and it was very easy understand and remember.
    Now I can really operate OSs.

    On question, Ubuntu is a distribution of Linux not Linux itself, and shell scripting is part of Linux. Why is the script preseded with the .sh extension?

    #18 Louis on 03.03.09 at 6:27 pm

    you don’t need the .sh extension but the extension helps you know that it is a shell script just by looking at the extension. It’s convention, but you could name it anything really.

    #19 midiangr on 05.19.09 at 12:34 pm

    thxn!

    #20 John Washburn on 07.08.09 at 8:10 am

    Thanks for the tut! I’ve been looking for a good place to start learning shell scripting. This really got me off to a good start.

    #21 John Washburn on 07.08.09 at 8:26 am

    For those who are still wanting more (like me) I found a great tut on Ubuntu’s website. http://tinyurl.com/r8pnuj

    #22 Duane on 08.03.09 at 5:54 pm

    I agree with rakudave. The statement in the blog post that reads, “Next, run this command to make the file executable” should read, “Next, run this command to make all users of your system (including you) able to read, write, and execute the file.”

    If the intention was just to make it executable, then an alternative to rakudave’s suggestion would be:

    chmod 755 samplescript.sh

    Which allows all users and groups to read and execute the file, but only the owner has write access.

    Leave a Comment