Author Archives: admin

Using the built-in Bash Special Variables

Bash Special Variables

Bash has a number of what they term ‘bash special variables’. Here is a list of the most useful ones I use fairly regular in scripts :

$? – Exit code

$# – Number of entered arguments/parameters to a script

$0 – Name of the script or shell executing the command

$1 (up to number of parameters entered) – Value of specified argument/parameter

$@ – Expands to be a lit of all the entered parameters

 

ok so a quick explanation of those bash special variables…

$? is used within scripts to determine the result from the previous command. For example if you enter an incorrect command immediately before you will get this :

# lss
-bash: lss: command not found
# echo $?
127

ls was the actual command I actually wanted to do which would show this :

# ls
wget-log
# echo $?
0

A number 0 in 99.9% of cases indicates the previous command was successful, anything else indicates a failure of some sort. The man pages for the command you are running may tell you more concise error numbers for that command so you can further customise your script.

$# is a particularly useful command I normally use for validating the number of parameters a user has entered to a script. For example if you have written a script which needs 2 parameters, and has an optional 3rd parameter as well then you can do this :

if [ $# -eq 3 ]
then
echo "3 parameters entered, continuing processing"
elif [ $# -eq 2 ]
then
echo "2 parameters entered, no date supplied so using today" # just example
elif [ $# -lt 2 ]
then
echo "You need to enter at least 2 parameters - exiting"
exit 1
fi

Clearly in that script the actions taken for each option can be tailored to your needs

$0 I generally use to form a base for log file names in conjunction with basename like this :

progname=`basename $0`
logfile="/tmp/logs/$progname.log"

$0 is the whole path to your script, basename takes just the script name part ignoring any directory names, then the logfile line determines where you are going to push your log entries to.

$1 (etc) is the value of a positional parameter. If you run a script like this as example :

./script1.bash 1 2 3 4

Inside script1.bash if you have this :

echo $2

Then you will see the value it displays is 2. Now you can see the relationship between $1 and $0 as all $0 is doing is displaying the first word of the command line you entered.

$@ can be used in a loop to rotate around all your entered parameters. Using the above script1.bash as example :

# cat script1.bash
#!/bin/bash

for i in $@
do
echo "Value : $i"
done

Run the script like this and here is the output :

# ./script1.bash 1 2 3 4 5
Value : 1
Value : 2
Value : 3
Value : 4
Value : 5

There are plenty of other Bash special variables that you may find useful, check out the ‘man bash’ help page to find them all or you can see them HERE.

Check out my other bash tips and tutorials HERE

Ipega 9028 Wireless Bluetooth Android TV Box Gamepad – Minix Compatible

Ipega 9028 Bluetooth Gamepad

ipega 9028 Bluetooth Gamepad

ipega 9028 Bluetooth Gamepad

A review of the Ipega 9028 Bluetooth gamepad. It is made with an attachment you can see in the pic above to attach your smartphone into it which is pretty cool if you want to use it like that! Personally I use my Ipega 9028 on my new Minix X8-H Plus.

Size and construction-wise it is perfect, feels strong, and has handled everything I threw at it. It has a usb connector to enable you to charge it (2 hours approx) and that charge lasts a few hours of constant playing. You can also play while it is plugged in and charging as well if you want.

Setting it up was easy (after learning the hard way). Do not bother with any of the Ipega drivers, sites, apps etc. Just activate bluetooth on your android device and pair it. The full steps I do are this :

1) set the controller to pair by pressing A and then the red Start button on the controller. The light will start to flash fast meaning it is looking for a connection.
2) on my Minix box I turned on Bluetooth and it finds the Ipega 9028 on the list.
3) click on it and it will pair in seconds.

You can buy the wireless Ipega 9028 controller from amazon same as I did here :

Then it is just down to which game you want to play and if it supports controllers/gamepads.

Check out my game reviews I have tested out with the Ipega 9028 on my Minix X8-H Plus HERE

PDF Utility pdftk – Making handling PDF files easy

You may have already read our previous article how to install PDFTK on your Centos or Red Hat server HERE so here are some of the uses for pdftk that you may find useful.

Merge multiple PDF files using pdftk

PDF Utility PDFTK makes it easy to merge any number of pdf files into a single output file. First here is how to merge 2 specific files :

pdftk input1.pdf input2.pdf cat output output.pdf

If you want to merge a whole list of pdf files you can use wildcards like this :

pdftk *.pdf cat output output.pdf

ok all pretty simple so far, so how about the reverse… you now have a multi page pdf file and want to split it into seperate single page pdf files. Here is how you do it :

pdftk bigfile.pdf burst

The above will generate multiple numbered output files each with a single page.

So now maybe you have multiple multi-page pdf files and you want to merge specific pages from them into a new output file. This is how it is done :

pdftk X=bigfile1.pdf Y=bigfile2.pdf Z=bigfile3.pdf cat X2-3 Y1-2 Z9 X12 output output.pdf

this will create output.pdf with pages 2-3 of bigfile1.pdf, followed by pages 1-2 of bigfile2.pdf, page 9 of bigfile3.pdf, and then page 12 of bigfile1.pdf.

There is much more functionality inside pdftk, but this gives you a quick insight into just some of the things it can do.

We hope you find this useful!

Installing pdftk on Centos 5 and 6 – PDF management and utility tool

Installing PDF management and utility tool pdftk on Centos 5 and 6

PDF files are not the easiest beasts to handle at the best of times, however there is a tool called pdftk which is freely available to make your life a lot easier!

PDFTK has the functionality to split, merge, watermark, and a variety of other useful tasks to make handling data much easier.

Version 2.02 is the most complete and flexible version and can be installed on Red Hat or Centos like this :

Right click and copy link address on the appropriate link below –

Red Hat Enterprise Linux (RHEL) 6 RPMs:
64-bit (x86_64)
32-bit (i686)
Red Hat Enterprise Linux (RHEL) 5 RPMs:
64-bit (x86_64)
32-bit (i386)
CentOS Linux 6 RPMs:
64-bit (x86_64)
32-bit (i686)
CentOS Linux 5 RPMs:
64-bit (x86_64)
32-bit (i386)

Then run a yum install like below (example below is for the 64 bit Centos 6 version, just use the correct rpm file from the above list) :

yum install https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk-2.02-1.el6.x86_64.rpm

Check it is installed correctly like this :

pdftk --help | more

pdftk 2.02 a Handy Tool for Manipulating PDF Documents

You can see from the output that you now have version 2.02 installed.

We have created a post with some of the uses for pdftk HERE

Bash script loop to monitor something like disk space

ilovebash

ilovebash

Loops in bash are something Ive covered before, including parsing a file record by record (HERE), but one of the other uses I have frequently is a never ending script which watches something changing.

Maybe a good example of this is if you are doing a large file transfer (scp as example) and you want to watch that the disk doesnt fill up before it finishes. The bash script loop WHILE is your friend for a quick and dirty way to show current space used. Here is how you do it.

Bash script loop to watch or monitor something constantly

As you may know already the command to view disk space usage is this :

df -k

The task you want to achieve is to save you keeping typing that again and again.

Here is the code to do it and show you the current time as well :

while [ 1 ] ; do

date
df -k
sleep 10

done

And thats it !! The while [ 1 ] is the never ending bash script loop (break out with a ctrl c) and then the script displays the date / time, shows the disk space, and sleeps 10 seconds and then loops again.

Want to make the date/time stand out a bit more? Reverse the colour of the screen on it by adding this :

while [ 1 ] ; do

tput smso
date
tput rmso
df -k
sleep 10

done

The tput smso reverses the text colours, and the tput rmso removes the reverse colours. There are more options for the tput command but this is simple and does the job.

If you liked this quick tutorial for monitoring something then share it with your friends, and if you want any more tutorials on different subjects then reply to this post or email me on admin@bashworkz.com

Bash Parse a file forwards / Parse a file in reverse (backwards)

ilovebash

ilovebash

Bash Parse a file forwards / Parse a file in reverse (backwards)

Bash shell has some very simple commands that can help you do everyday tasks. One of the main functions I need a script to do is to read each line in a file from beginning to end and do something with each line the script reads.

Say for example you have a file called file1.txt of bank withdrawals like this :

2015-01-02 Withdrawal 100.00
2015-01-03 Withdrawal 10.00
2015-01-05 Withdrawal 50.00
2015-01-09 Withdrawal 20.00
2015-01-12 Withdrawal 110.00
2015-01-08 Withdrawal 200.00

ok thats a very simple file but you get the idea.

Part 1 – Parse a file forwards in Bash

So your task maybe is to find all transactions where you took out more than 100, the easiest way is to parse the file record by record and output the records you want to see.

cat file1.txt | while read LINE ; do

# the echo "$LINE" displays the whole record, the cut -d" " -f3 uses space as delimiter and gets the 3rd field. it then puts the value in a variable called VALUE1
VALUE1=`echo "$LINE" | cut -d" " -f3`

# Now you need to make an arithmetic decision based on whats in VALUE1 variable
if [[ $VALUE1 -gt 99 ]]
then
# if your value is greater than 99 (therefore 100+),
# you could also have done if [[ $VALUE1 -ge 100 ]] which means is greater than or equal to

# Now display the whole record
echo "$LINE"
fi

done

So that reads file1.txt line by line, the while read LINE ; do puts the current record into a variable called LINE, which inside the loop has extracted part of it into the VALUE1 variable, from where a decision process is made and ourpur displayed. And the done at the bottom ends the loop.

Part 2 – Parse a file in reverse (backwards) in Bash

Now maybe you want to adjust the above script to show your latest transactions first instead of in the order they are in the file. There are 2 VERY SIMPLE ways to do this (maybe more even), you could sort the file in reverse, but there is actually a much simpler method… check the script below and see if you can spot it….

tac file1.txt | while read LINE ; do

# the echo "$LINE" displays the whole record, the cut -d" " -f3 uses space as delimiter and gets the 3rd field. it then puts the value in a variable called VALUE1
VALUE1=`echo "$LINE" | cut -d" " -f3`

# Now you need to make an arithmetic decision based on whats in VALUE1 variable
if [[ $VALUE1 -gt 99 ]]
then
# if your value is greater than 99 (therefore 100+),
# you could also have done if [[ $VALUE1 -ge 100 ]] which means is greater than or equal to

# Now display the whole record
echo "$LINE"
fi

done

Do you see it?? The first word has gone from cat, and become tac !! tac is cat backwards!

Thats a very simple script, but as someone who has done bash scripting for over 20 years, I can honestly say that a very large portion of my scripts in one way or another will have a loop very similar to that in it!

If you liked this tutorial share it with your friends, or check out my other bash tutorials HERE

Or if there are any other tutorials you would like me to write then reply to this post, or email me on admin@bashworkz.com

Easy install ffmpeg on Linux – The ultimate media handling utility

Easy Install FFMPEG

Easy Install FFMPEG

Easy install ffmpeg on Linux – The ultimate media handling utility

Anyone who has tried to handle media files of any sort from images to movies, or also flv, mp4, mp3, ts or other types of multimedia content will have heard of the utility called FFMPEG.

Quite simply FFMPEG is the gem that gives you the ability to broadcast/stream, convert, or play that media file.

FFMPEG Functionality - The ultimate media tool

FFMPEG Functionality – The ultimate media tool

The biggest issue with this utility is that it has LOADS of seperate components, some new, some old, some obselete, some which may not even be available for your distribution of Linux.

There are 1001 different tutorials over the last 10 years for every different distribution of linux you can think of, but 95% of them are out of date, cumbersome, and after a lot of work to install it to the end, then you will find something that just doesnt work for you. ANNOYING!!!

WELL, having faced this uphill struggle myself, I found a simple 5minute solution that ANYONE CAN DO !!!

One guy has done the hard work for you and made a PRE-COMPILED version you can easy install ffmpeg by a simple UNZIP, put in your path (/usr/local/bin will normally do), and just run it !!!

His builds contain everything that I have ever needed so far. Each archive includes ffmpeg, ffmpeg-10bit, ffprobe, ffserver, qt-faststart, and all the man pages in both text and pdf format

Easy Install FFMPEG

Check his download site HERE

Dont forget if you find some use to his builds to send him some beer tokens !! At the end of the day he has saved you a LOT OF HOURS pulling your hair out !!!

Mr Robot – Episode 10 (of 10) – Series 1 Finale

Mr. Robot TV Series

Mr. Robot TV Series

Watch Mr Robot TV Series

Young, anti-social computer programmer Elliot works as a cybersecurity engineer during the day, but at night he is a vigilante hacker. He is recruited by the mysterious leader of an underground group of hackers to join their organization. Elliot’s task? Help bring down corporate America, including the company he is paid to protect, which presents him with a moral dilemma. Although he works for a corporation, his personal beliefs make it hard to resist the urge to take down the heads of multinational companies that he believes are running — and ruining — the world.

We hope you have enjoyed the first Series of Mr Robot! If and when there is a new series then we will bring it to you on bashworkz.com

Mr Robot – Behind the scenes interview with Carly Chaikin

Mr. Robot TV Series

Mr. Robot TV Series

Watch Mr Robot TV Series

Young, anti-social computer programmer Elliot works as a cybersecurity engineer during the day, but at night he is a vigilante hacker. He is recruited by the mysterious leader of an underground group of hackers to join their organization. Elliot’s task? Help bring down corporate America, including the company he is paid to protect, which presents him with a moral dilemma. Although he works for a corporation, his personal beliefs make it hard to resist the urge to take down the heads of multinational companies that he believes are running — and ruining — the world.

Mr Robot – Behind the scenes interview with Christian Slater

Mr. Robot TV Series

Mr. Robot TV Series

Watch Mr Robot TV Series

Young, anti-social computer programmer Elliot works as a cybersecurity engineer during the day, but at night he is a vigilante hacker. He is recruited by the mysterious leader of an underground group of hackers to join their organization. Elliot’s task? Help bring down corporate America, including the company he is paid to protect, which presents him with a moral dilemma. Although he works for a corporation, his personal beliefs make it hard to resist the urge to take down the heads of multinational companies that he believes are running — and ruining — the world.