User:Tsu2/Really Basic Stuff Scripting

Jump to: navigation, search

This page is for the newbies who haven't written their first script yet.

Creating My First Script

  • Create a file

Some applications like Vim (a standard console text editing app) will automatically create a file if you specify a not yet created file name in the beginning and then save. For example the following which will print "Hello World!" when executed

vi MyFirstScript.sh
echo "Hello World!"
:x

You can also create an empty file using "touch." For example the following

touch MyFirstScript.sh
  • Make the file executable

There are a few ways you can do this, both from the command line and by using a graphic file manager application and setting in the file's properties. Likely the easiest is to run the following from a console

chmod +x MyFirstScript.sh

That's all you need to do for your first script!
You can now either use a console app like vim or a graphical text editor (common apps in openSUSE include Kwrite, Kate, Leafpad) to edit this file, adding or modifying content.

  • Executing "My First Script"
    • open a console if one isn't already open, it can be Konsole, LXterminal, xterm or something else
    • If your console doesn't open automatically to where your script is, you can execute your script by specifying the path. The following example assumes your console opened in your /home directory but your script is in the Documents folder
Documents/MyFirstScript.sh

If your console opens where your script is, you still need to specify "same directory" as follows

./MyFirstScript.sh

Many Commands

You can string together multiple commands into a single command with "&&"
For example

echo "First Command" && echo "Second Command" && echo "Third Command" && echo "Fourth Command" && echo "Fifth Command"

Breaking a string of commands into multiple lines

The above example can become difficult to read if the commands themselves were very long and complex.
You can still string together multiple commands into a single command by "escaping" breaks as follows using a back-slash

echo "First Command" && \
echo "Second Command" && \
echo "Third Command" && \
echo "Fourth Command" && \
echo "Fifth Command"

Script Creation Tools

Shell Check

An online tool which checks your code in real time. Although you connect to a website to get the webpage, the code you're debugging runs entirely locally in your web browser.