<
BASH SCRIPTING

Back to Coding Stuff Page

BASHING YOUR WAY - no hello world examples on this page

Bash is the shell, or command language interpreter, for the GNU operating system.

The name is an acronym for the ‘Bourne-Again SHell’

Use any Text Editor to create your bash files.

Nano can also be used to create the bash files.

Some Basic Commands and Terms:

#! = Shebang. Indicates which interpreter a script should be run with.

# = comments, does not execute anything this is for your notes

echo = Print a message to the screen.

which = Tells you the path to a particular program.

$ = Placed before a variable name when we are referring to it's value.

date = Prints the date.

+, -, /*, / = addition, subtraction, multiply, divide

var++ = Increase the variable var by 1

var-- = Decrease the variable var by 1

% = Modulus (Return the remainder after division)

Find the BASH path by typing in any of these commands:

which bash

or

echo $SHELL

Make the script file executable:

chmod 755 yourscript.sh

Execute your script files:

./hello.sh

or

bash hello.sh

Now for the good Stuff! The Examples

Create a template script file. You can re-use it when starting a new script file.

Name it: starter.sh or robbase.sh (named after the rapper)

!/bin/bash

# comments and express yourself

#variables

# input

#action

#output

# the end all rights reserved....fuck that...the Internet is free!

Create a template script file with a script file. You can re-use it when starting a new script file. This will ask for the name of the file, create it, set permissions, and ask if you want to launch it.

master.sh

#!/bin/bash

# comments

#variables

# input

echo "what is the file name?:"

read filename

#action

cp ~/Documents/Myscripts/starter.sh $filename.sh

touch ~/Documents/Myscripts/$filename.sh

chmod 755 ~/Documents/Myscripts/$filename.sh

echo "open file now?: y or n"

read varopen

if [ $varopen = y ]

then

nano ~/Documents/Myscripts/$filename.sh

else

echo "yo file created, knowledge is power"

fi

#output

# the end all rights reserved....fuck that...the Internet is free!



Print a list of names on the screen

names.sh

#!/bin/bash

# this a file to list the names

echo aniyah nikki ally samantha rose



Here are 3 bash files, this will Set a variable, Display the variable, Export the variable.

So by running ./onefile.sh all 3 bash files will run

onefile.sh - first bash script

#!/bin/bash

# start first bash script will run the second and third file all at once

number=23 #set variable

echo $number #display var

echo "************1"

export number # send var to next file

bash twofile.sh # run the second bash file

twofile.sh - second bash script

#!/bin/bash

numberx=25

echo $number #display var from first bash file

echo $numberx

echo "********************2"

export number # send var to next file

export numberx # send var to next file

bash sum.sh # run the 3rd file export

sum.sh - third bash script

#!/bin/bash

# two numbers export to here

echo $number

echo $numberx

echo "yeah buddy"

echo "**********************3"



Get information from a user and display it:

info.sh

#!/bin/bash

# get some information

echo Hello

echo Please Tell me some info:

echo Enter Username:

read varname # holds the input

echo What is your favorite vacation spot?

read varvac # holds the input

echo "It\'s nice to see you $varname, again. Have fun in $varvac



Find out how long a variable is:

varlength.sh

#!/bin/bash

# Show the length of a variable.

a='Yo man who shot the sheriff'

echo ${#a} # will display 27 counts spaces

b=49532342554

echo ${#b} # will display 11



Copy the last 5 lines of a file into another file:

copylines.sh

#!/bin/bash

tail -5 /etc/passwd > file_passwd

# read the file

cat file_passwd



FOR LOOP to display the whole list:

#!/bin/bash

# read the list and display the name, do until the end of the list

#variables list of names

list='pam lindsey jill mary brenda rose'

#action girl the var to holds name as if reads the list

for girl in $list

do

echo "yo $girl how are you, call me"

done

#output

echo " hoola!"

# the end



FOR LOOP find html files and read name them to a text file:

#!/bin/bash

# set var to a folder

#variables

folder=/pam/Documents/testx

for fileconv in $folder/*.html

do

echo $fileconv #use to check the value for html

# ls $fileconv

cd ~/Documents/testx

pwd

cp $fileconv $fileconv.txt #copy them to folder

done

#output

echo thats all folks

# the end



FOR LOOP and IF with a break on a keyword:

#!/bin/bash

# for with a if and break on a keyword

#variables - take out or replace samantha to not find her

list='ally kate pam samantha nikki mary'

# input

echo "hello if there is a samantha stop"

#action

for girl in $list

do

if [ $girl = samantha ]

then

echo found a samantha

break

fi

echo no samantha found yet

done

#output

echo we found a samantha and stopped

#end



IF basic IF script:

#!/bin/bash

# yo if statements are the best

# set variables

bet="Hawks vs Packers"

amt=220

win=yes

# if to determine outcome

if [ $win == yes ]

then

sum=$(($amt+200))

echo "Winner of $sum from $bet bet"

fi



BOOLEAN and a IF with a OR script:

#!/bin/bash

# boolean and IF with OR

#input

input

echo "Yo enter in who do you call first Pam or Kate or someone else"

read vargirl

#action

if [ $vargirl = "kate" ] || [ $vargirl = "pam" ]

then

echo "good choice with Kate or Pam !!!!"

else

echo "who the hell is that?"

fi

#output

echo call ghostbusters

# the end



Many IF and ELSE:

#!/bin/bash

# if then else and again

#variables

hsex=n

# input get a yes or no

echo will you be leaving the country tonight?

read varanswer

#action

if [ $varanswer = y ]

then

echo where will you be going?

echo enter 1 "for" Poland or 2 "for" Japan?

read vargirl

if [ $vargirl = 1 ]

then

echo Poland it is Have fun

else

if [ $vargirl = 2 ]

then

echo "Japan go get some Sushi"

else

echo "no you stay home then "

fi

fi

fi

#output

echo later vader

# the end



Use CASE command to get input and display the choice:

#!/bin/bash

# case list instead of a bunch of iffy statements

#variables

vartitle=Yeah Buddy

# input

echo "which girl do you want meet with?"

echo 1-Pam Anderson

echo 2-Kate Upton

echo 3-Ally Smith

echo 4-Nikki Benson

echo 5-none at this "time"

read choice # holds the input

#action

case $choice in #based on the number display the proper message

[1])

echo "Pam a lamb, awesome!" $vartitle

;;

2)

echo "Kate and do the cat dance" $vartitle

;;

3)

echo "Ally be good to all"

;;

4)

echo "Nikki make my day"

;;

5)

echo "no one, day off today"

;;

*)

echo "Invalid choice code me"

;;

esac



FUNCTIONS, use a function to display a list:

#!/bin/bash

# read the list of beers and display them

#variables

beerlist='Strohs Coors Bells Atwater Founders Red Stripe'

#action function is beer read the list and display all

beers () {

echo $beerlist

return 0

}

beers

echo $? # value of return

# the end



FUNCTIONS and VARIABLES in and out of functions:

#!/bin/bash

# this is functions read variables

#function inside when this is called it will change varibales

inside () {

local varsat='local guy' #only changes in here

echo "inside function varsat is" $varsat "varsat2 is" $varsat2

varsat='xaiver' # change has no effect because of local cmd

varsat2='mary'

}

varsat='global weather'

varsat2='global mess'

echo "before function varsat is " $varsat "varsat2 is" $varsat2

# call function inside

inside

# uses var values and varsat stays what it was outside the function

echo "after function varsat is " $varsat "varsat2 is" $varsat2

# the end



GAME to Guess the number:

#!/bin/bash

# script to guess number range

echo Enter Your Name:

read nam

echo Enter your Number:

read num

# echo $nam , $num

if [ $num -lt 10 ]

then

echo "$nam your number is less then 10"

elif [ $num -lt 20 ]

then

echo "$nam your number is less then 20"

elif [ $num -lt 30 ]

then

echo "$nam your number is less then 30"

else

echo "$nam your number is higher then 31"

fi

echo good bye



PS3 to select from a menu list:

#!/bin/bash

# select name from list and display chiceabd

#variables

beers='Lite Coors Molson Labatt Pabst'

# input

PS3='Pick One Beer: '

#action

select beer in $beers

do

if [ $beer == 'Quit' ]

then

break

fi

echo Hello $beer

done

echo Bye

# the end




Email comments to: Webmaster@23cyber.com*****© 2013 23cyber.com All Rights Reserved.....Something very special.....