Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bash Scripting Tutorial
#1
So everybody is doing there little .bat files for windows, right. Well what is the equivalent in linux? Typically the shell installed with linux is bash(bourne again shell, it's a joke l'll explain it to you one day). Ok so here is how you write a bash shell, open your favorite text editor, mine is vi and type the following.
Code:
#!/bin/bash
#The hash mark "#" denotes a comment, however on the first line the comment is also defining which
#shell / scripting language we are using
echo -n "The date: "
date
#echo  prints what is in the quotes, the -n says not to start a new line
#date is the command to show the date
#so bash works very similar to bat, you can take user input in bash and use if statements in bash
echo -n "Which user to finger:"
#the \n means a new line, the  \c is to take user input on the same line
read USERNAME
finger $USERNAME
#this fingers the username you have input
echo "Type the number 1:"
read TYPEDNUMBER
if [ $TYPEDNUMBER = "1" ];
then
#this says if the typed number = 1, and the line below it says, then
echo "correct"
#print correct
elif [ ! $TYPEDNUMBER = "1" ];
then
#the line above is a truncation of else if, but think of it as if the number doesnot = 1
#if you are wondering where the does not comes it is the exclamation point, sometimes called bang
echo "Wrong"
#print wrong
fi
#fi ends your if statement
Save this file as "test.sh".

To run the script without chaning your unix permisions you can type: bash test.sh
In the directory it is in. Or if you want to change your unix permissions and run the script traditionally you can type:
chmod 755 test.sh
Then you can run it the normal way which is to type:
./test.sh
Make sure your in the same directory. If you are not in the same directory you can run it by typing the full path, like this(but with your path)
/home/lane/scripts/test.sh
Ok so a couple caveats with this thing. 1) your nix enviroment has to have bash, but most do and 2) your bash must be in /bin/bash which for most nix machines, that is where it is. But like on my machine which is a little diffrent I have to have this as my first line

#!/usr/local/bin/bash
because my bash is in /usr/local/bin/bash.

3) Make sure finger is on your system Smile and you type a username that is logged in Smile

And this won't run the same in all environments, and if you copy and paste it make sure you pay attention to the punctuation and make sure it didn't change, before I tested this I wrote it on a windows machine in word and when I pasted it in vi in my putty ssh session some of the punctuation changed. Another thing is, if you just copy and paste it you may have problems because of something called magic quotes. The quotation marks may change to the kind that lean inwards, and bash see's that as just a letter not as a quotation marks, so if your not typing it in(which you should think about doing since it helps the learning process) and it just prints the script out, instead of running it replace the quotation marks manually.


Bash is a lot more powerful than .bat, and when I do get stuck on windows systmes, I install a program called cygwin, so that I have a bash enviroment on my windows machine and can automate things like backing up a drive, with tools like rsync.

Alright have fun.
Reply
#2
Thanks for this bsdpunk, I need to learn about bash and this helped me with the first step!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [UNSOLVES] find [Bash] i586 0 714 01-16-2012, 04:10 PM
Last Post: i586
  .Bashrc (Bash) Scorpion 1 674 01-15-2010, 05:26 PM
Last Post: Salmon

Forum Jump:


Users browsing this thread: 1 Guest(s)