This script is designed to show a number of recent commits to the users current SVN branch in an easy readable format.
Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts
Sunday, 10 August 2014
Host Information
This script is designed to retrieve host information from the kernel to the memory. It assigns standard commands to variables and then echo's everything out in a nice readable form.
IP Telnet
This script is your basic Telnet script.
In order to use this script you must create a text file called 'ip' and fill it with the ip(s) you wish to connect to.
In order to use this script you must create a text file called 'ip' and fill it with the ip(s) you wish to connect to.
Pack Up Tar Files Script
This script is used to pack up files into a "tar.gz", this can easily be change to a "tar.xz" by replacing all instances of "tar.gz".
Watcher Script
You just need to set this script to run on startup and there you go, if anyone tries to connected to your pc via ssh,
Friday, 11 May 2012
Slightly more advanced guide to Shell Scripting
Hey guys,
In this post I hope to go through a few more advanced options when writing scripts. I'm just going to cover things like methods, parameters and a few other tricks which can make your script look pretty cool.
Methods
As a Java developer by trade I use methods quite a lot, programming without them is sort of odd, for those who don't know a 'method' or 'function' in basic terms is a section of code which can be called at a later time, you can probably see why this would be useful.
Say you have a menu system in your script where the user must choose an option for the script to continue, each option would produce a different outcome, this is a good example of when you would want to use methods.
Examples
#!/bin/bash
echo "***************EXAMPLE SCRIPT***************"
echo "Please choose and option:"
echo " 1 = option A"
echo " 2 = option B "
echo " 3 = option C "
read option
if [[ "$option" == "1" ]]
then
OPTIONA
exit 0
fi
if [[ "$option" == "2" ]]
then
OPTIONB
exit 0
fi
if [[ "$option" == "3" ]]
then
OPTIONC
exit 0
fi
OPTIONA () {
echo "Hello world, this is option A"
}
OPTIONB () {
echo "Hello world, this is option B"
}
OPTIONC () {
echo "Hello world, this is option C"
}
Ok so I'm going to dissect this and explain what each part is doing, its quite simple really.
echo "***************EXAMPLE SCRIPT***************"
echo "Please choose and option:"
echo " 1 = option A"
echo " 2 = option B "
echo " 3 = option C "
This part is the actual menu which will be shown to the user, we use the 'read option' to read the input from the user and assign it to the variable 'option', this is so we can use it later in the if statements.
if [ "$option" == "1" ]
then
OPTIONA
exit 0
fi
if [ "$option" == "2" ]
then
OPTIONB
exit 0
fi
if [ "$option" == "3" ]
then
OPTIONC
exit 0
fi
Here we have the if statements, quite straight forward really, what's happening is we are calling the 'option' variable, if this equals '1' for example then we call the OPTIONA method.
OPTIONA () {
echo "Hello world, this is option A"
}
As you can see option A echo's out the line "Hello world, this is option A". After each method the code 'exit 0' is run, this is just to exit the script cleanly.
Parameters
A good example of parameters would be if you wanted to write a script to remove all '.txt' files from a directory. In order to do this you could write a script which takes in the directory path as a parameter. This makes the script more flexible, it gives the user the choice of which directory input. The bash shell supports up to 9 parameters which are represented in the script as '$1' (if you remember awk this might make more sense) 1 being the number of the parameter.
Example
#!/bin/bash
(cd $1 && rm *.txt)
This is a very simple example, to run this script (lets call it remove.sh) you would do the following:
./remove.sh /home/zdrenka/pictures
The script would then assign ' /home/zdrenka/pictures' to '$1' and that's what we call a parameter, there are loads of useful ways in which to use this facility.
Since I started scripting I have picked up bits and bobs which can make your script look pretty cool, one of which is colours. Colours can make a script look that little bit more impressive. When I write a script one of the first things I do is paste this at the top:
################################################
# Define colours
################################################
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
txtred=$(tput setaf 1) # Red
txtgrn=$(tput setaf 2) # Green
txtylw=$(tput setaf 3) # Yellow
txtblu=$(tput setaf 4) # Blue
txtpur=$(tput setaf 5) # Purple
txtcyn=$(tput setaf 6) # Cyan
txtwht=$(tput setaf 7) # White
txtrst=$(tput sgr0) # Text reset
the '#' are obviously comments. I wrote this a while ago and now have it saved as a template, I use the colours to highlight errors, its very useful when you have a lot of text on the screen. to use these colours we just add them to the text as shown below:
echo "${txtbld}${txtred}ERROR File does not exist ${txtrst}";
The 'txtrst' variable will reset the text back to its original colour, you need this otherwise all text below your statement will be red.
Another useful command I have come across is the 'set -e' command. Put this at the top of your script and the script will automatically quit if it comes across and error, this is good practise as if you hit an error its not usually a good idea to carry on with the script. Although if you would like to have this command skip a certain part of the code you can surround that part with 'set +e' and 'set -e'. for example:
set +e
cp *.txt *.jar *.war /home/zdrenka/Java
set -e
Without these my script would stop once it hit this cp command as there may not be any '.war', '.txt' or '.jar' files in my Java directory which would technically throw up an error.
I hope this quenches your thirst for knowledge!
Sunday, 4 March 2012
Beginners Guide to Shell Scripting
It has come to my attention that a lot of people need a beginners' guide to shell scripting, therefore I am obliged to provide one.
The shell
The Terminal
The Console
Shell scripting in Linux isn't to difficult to master, although it helps if you have some programming background. We'll start by explaining the shell.
The shell
The shell is a program which actually processes commands and returns output. Bash is the most popular Linux shell as it contains standard features such as foreground and background processes and command history.
A question that is asked a lot is: "what's the difference between the Console and the Terminal?"
The Terminal
The Terminal is a wrapper program which runs a shell. This was once a physical device which consisted of not much more then a monitor and keyboard. As time moved on this became software and can be used to access the shell and run commands.
The Console
The Console is a special kind of Terminal, the Console used to be used for low level communication with the operating system. These days it is used in much of the same way as the Terminal.
In conclusion there isn't really much difference between the two, they both have separate pasts but are now essentially used for the same thing.
Writing Scripts
I recommend using Sublime to write your scripts as it has a lot of great tools to make use of, although if you are unfortunate enough to be using a Windows system I recommend Notepad++.
At the top of each script the we have a Shebang line. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. All scripts under UNIX and Linux execute using the interpreter specified on a first line. Then we would have the command we wish to execute.
#!/bin/bash
echo Hello Linux!
The above script when run will display the words "Hello Linux" in the console/terminal. It uses the echo command which does what it says on the tin, it will echo out the words Hello Linux!.
This is one way of doing it, however the use of variables is more elegant. A variable stores data in memory and can be called at different times within the script, it can also be changed and amended throughout the script. Below is an example of Hello Linux with variables.
#!/bin/bash
VAR="Hello Linux!”
echo $VAR
Next we must learn how to use the chmod command, this is use to set the permissions for the script we have just created.
Chmod
the chmod command (change mode) is a command that lets the user control how much access it should permit to a file. I have a post with more details on this command here In order to run our script we must first use chmod to make it executable to do this I use the command
chmod +x helloLinux.sh
the +x makes the script executable for ALL users, if you just want it to be exe for you then u+x is for you.
After this command a simple ./helloLinux.sh will run the script.
Hopefully this should give you a good grounding for writing your own shell scripts. For a more advance guide try here
Any questions are welcome!
Subscribe to:
Posts (Atom)