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.

About Jack Woods

I'm a teacher, writer, traveler, and designer. I'm into alternative medicine and I like designing programs and devices to make life easier.
This entry was posted in Louie's Best, Ubuntu and tagged . Bookmark the permalink.

42 Responses to Writing a Basic Ubuntu Script

  1. ben says:

    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 says:

    Oops, thanks. I made the changes.

  3. Dan says:

    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 says:

    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 says:

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

  6. David says:

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

  7. David says:

    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 says:

    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 says:

    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 says:

    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 says:

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

  12. HellYeah says:

    Hell yeah! Thanks!

  13. Vicco says:

    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 says:

    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 says:

    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 says:

    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 says:

    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 says:

    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 says:

    thxn!

  20. John Washburn says:

    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 says:

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

  22. John says:

    This has got to be one of the worst tutorials I’ve ever seen.

    “Ubuntu script” ??
    “chmod 777 samplescript.sh”

    YOU SHOULD BE ASHAMED TEACHING PEOPLE THESE THINGS.

  23. Hustle says:

    Thanks Very Much.
    Works Brilliant! :)

  24. Duane says:

    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.

  25. Paul says:

    Thank you. I am trying to learn a lot of different things all at once in Ubuntu. Other tutorials assume that I already know all this stuff. Thanks for doing this!

  26. SteamRoller says:

    That was a awesome tut it help so much!!!!

  27. ghe says:

    i like it,wherez the rest?

  28. Rocket says:

    Just stumbled upon this, and well, I don’t like it. Here’s why.
    1) What is an “Ubuntu Script”? This is called a “Shell Script”. It runs all Linux distros, not just Ubuntu.
    2) ‘chmod 777′ is not the command you are looking for. That allows all users to read, write, and execute the file. The command you want is ‘chmod +x’.

    Other than that, this is a good start for people who know nothing about Linux or shell scripts.

  29. anirban says:

    thnks so much.. i used fedora, but ubuntu is the best.. iwhen i cant do the shell programing i thought i wll switch into fedora.. but yr help, make me use ubutu.. thnks so much……..

  30. Parag says:

    great Tutorial thanks, helped me a lot

  31. Sarathj says:

    I ma new to linux. This is a very good start to me.

    Thanks

  32. MitchE says:

    Thanks for this tutorial, it helped me helped tremendously. Easy to modify for whatever you are trying to do and easy to understand.

  33. Vasyl says:

    Thank you, great post. But I still have questions…

    Is it possibly to create in Ubuntu something like HTA-application (in Windows)? I need to create some window, process data (if possibly by using JavaScript) and display result as HTML.

    What can be as replacement for
    ActiveXObject(“WScript.Shell”)
    ActiveXObject(“Scripting.FileSystemObject”)
    ActiveXObject(“Msxml.DOMDocument”)?

  34. Gaurav Sood says:

    Great tutorial!!!

    :-)

  35. Chase says:

    suppose i wanted it to run at start up. My goal is to start up two virtual machines when the computer starts up.

  36. jake says:

    great – short and sweet!

  37. Sergey says:

    Hi,
    I followed your tutorial.
    Saved the samplescript.sh file to the desktop and when I click on it it is not starting Firefox.
    It is starting the gedit text editor with following text:
    #!/bin/sh
    echo “welcome”
    firefox

  38. youssef says:

    I have aproblem with application called Opmanager
    when I run it and I closed the terminal the process for this application stoped I run it in Background but the same problem still persist what can I do?
    thanks

  39. Grandfather Time says:

    Sergey, your problem is that you are not running the script from the terminal, you are simply opening a text file….

    If you are using Ubuntu you need to click Applications –> Accessories –> Terminal

    Now that your terminal is open, type:
    chmod 755 samplescript.sh
    and then hit enter.

    This sets permissions for the script so that only you can write to the file, while still letting all users read and execute it.

    While still in your terminal, type:

    ./samplescript.sh

    to run the script.

  40. rossi says:

    Good introduction. I am migrating from Dos .bat to Ubuntu, it helped me getting started.
    Some of the comments show why it’s difficult to get easy solutions in the diverse world of Unix. A lot of Unix insiders assume you have to get to their level of understanding first, before you might do anything useful.

  41. tom says:

    thanks i managed to make a script to install codeblocks
    then run it :)

  42. rasool maleki says:

    Thanks a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>