• Home
  • Shell
    • Emacs
    • Perl
    • screen
    • sed
  • Ubuntu
    • VNC
  • Web Development
    • Javascript
    • Joomla
    • MySQL
    • osTicket
  • Windows
    • Gimp

Posts in category BASH

BASH> Hello World Script

Oct26
2010
Written by Scott Rowley

So you’ve either recently come to the Unix world or you are finally getting around to some shell scripting to ease your daily operations (Say hi Jim!). So for starters what *IS* bash scripting? Scripting in general is making a program or “script” do all or most of your work for you. For instance if you wanted to find every instance of the word “hosting” in a particular file that is 30,000 lines long. That would take a LONG time doing that by hand and you would likely miss several along the way (being a fallible human and all). So instead we would use a grep (or similiar command) from the command line.
Well that may be easy enough for just one command but what if we then need to check all of those lines that are found and seperate them by date or some other criteria, then sort them out etc? For this we will use scripting. We can also use scripting for events that we want to take place every single day, whether it be simple or complex. This can be done via a cronjob, but we can get to that in a later article. So first off we’ll need to find where our “bash” resides…

which bash > helloWorld.sh

“which bash” will give us the location of the bash that is being used within our path. The > will send that result into a new file titled “helloWorld.sh” that we will use for our script. Now if we open up that file we will have something like the following:

/usr/bin/bash

Nothing else should be in our file at this point. This is good but not quite what we need. We need to add #! in front of this. This tells our script that we will be using /usr/bin/bash as our shell to run the rest of this script.

#!/usr/bin/bash

To complete our very first script all we’ll need to do is enter in the following a few lines (doesn’t matter how many) after our #!/usr/bin/bash line, like so:

#!/usr/bin/bash
echo "Hello World!"

Now we have our script ready, we just need to execute it. There are two ways to accomplish this, first we can simply chmod the file to give it the execute (x) permission. For simplicity sake we’ll say its:

chmod 755 helloWorld.sh

The 7 sets it so that the owner (you) can do anything with the file, the first 5 sets it so that anyone in its group can read and execute it and likewise for the last 5 but for this is for everyone, not just the group. More on chmod and chown in future articles.

Now that we have our file chmod’d to be executable we can run it right from where we are by typing

./helloWorld.sh

and our output should be simply:

Hello World!

As mentioned there are two ways to execute the file. For the second method we could type the following and not have to worry about permissions (only ownership — but since we own it we really don’t have to worry about that either).

/usr/bin/bash ./helloWorld.sh

And, again, our output would be the same:

Hello World!

Congratulations, you’ve just made your first Bourne Again Shell (Bash) Script.

Tagged chmod, command, echo, hello world, script

BASH> Get all but the last field

Oct26
2010
Written by Scott Rowley

Recently I had need of cutting up domain names such as domain.com, this is easy enough for as simple a domain as this. However, the problem arises when you need to cut up something more like “education.k12.ia.us”. In order to get the extension alone in either case you can do the following:

echo domain.com | awk -F. {'print $2'}
com

Now normally if you have a domain as simple as example.com with only one period then you can can just do the opposite of this and print $1:

echo domain.com | awk -F. {'print $1'}
domain

The problem with more complex URLs is that awk’s field delimiter is now looking at multiple periods to look at, could be just one or it could be two, three, four or more.

To get the final extension in this case then we’ll use the following:

echo education.k12.ia.us | awk -F. {'print $NF'}
us

Then for the rest of it we’ll have to reverse it, then cut off the first field then reverse it again:

echo education.k12.ia.us | rev | cut -d. -f2- | rev
education.k12.ia
Tagged awk, delimiter, domain, extension, field

BASH> if ARRAY [does not] contains string

Oct26
2010
Written by Scott Rowley

Check if an array contains a given string:

teams=(chiefs broncos chargers raiders)
if [[ ${teams[*]} =~ broncos ]]
then
echo "VICTORY!"
fi

Or, if you don’t want it to contain the string then:

teams=(cardinals 49ers seahawks rams)
if [[ ${teams[*]} =~ broncos ]]
then
echo "This conference sucks"
else
echo "VICTORY!"
fi
Tagged array, contain, string

BASH> Set DELETE key in Putty/Poderosa/SecureCRT

Oct26
2010
Written by Scott Rowley

For Poderosa/Putty
– Create or edit the ~/.inputrc file with the following code:

"\e[3~": delete-char

For SecureCRT

Click Tools
Keymap Editor
Click the "DEL" button
Click Map Selected Key
enter in "\e[3~" (no quotes)
Hit OK
Save
Tagged delete, key, poderosa, putty, securecrt, terminal

BASH> Run command from root as another user

Oct26
2010
Written by Scott Rowley

Every once in a awhile you may find you need to run a command as a different user than yourself. In order to do so you will first need to be root, then perform the following:

su <username> -c '<command1>; <command2>;'
Tagged command, root, su, user

BASH> Redirect output to standard out AND file

Oct26
2010
Written by Scott Rowley

Eventually in scripting you are likely to want to output to both the screen (standard out) and to a file. This makes it convenient to see whats getting “logged” without needing the extra step of cat’ing the file.

For example:

ls -al | tee ls_al_result.txt

This will give you your normal ‘ls -al’ results but also feed them to your new file.

Tagged ls, redirect, standard out, tee

BASH> Tail multiple files & highlight

Oct26
2010
Written by Scott Rowley

Tail multiple files at once:

tail -f log1.log log2.log

If you want to highlight something when doing ‘tail -f’ you can use the following command:

tail -f /var/log/logfile | perl -p -e 's/(SEARCH)/33[7;1m$133[0m/g;'

Or if your terminal supports colours, such as linux terminal, you can use this:

tail -f /var/log/logfile | perl -p -e 's/(SEARCH)/33[46;1m$133[0m/g;'

And if you want it to beep (annoy you!) on a match use this:

tail -f /var/log/logfile | perl -p -e 's/(SEARCH)/33[46;1m$133[0m07/g;'

Another option using sed instead:

tail -f /var/log/logfile | sed "s/(SEARCH)/^[[46;1m1^[[0m/g"

Note that in the last example you have to actually type “cntl-v cntl-[” in place of “^[”

For a full list of control characters on Linux see:

man console_codes
Tagged highlight, sed, tail
Newer Entries »

Corrections? Questions? Comments?

Find an error?
Everything work out great for you?
Have some feedback?
Like to see something added to the article?

PLEASE leave us a comment after the article and let us know how we are doing, or if something needs corrected, improved or clarified.

Thank you!
- The Management

Advertisement

Sudo Bash
By Geeks - For Geeks

Back to Top