Bash Scripting
#!/bin/bash -- this string causes the script to be run from the /bin/bash shell
Executing a script
sh scriptname - if the script is not executable, use the shell to interpret the script's contents
#!/bin/bash and chmod +x script - The #!/bin/bash should be the only thing in the first line of the script. you should chmod +x scriptname and then execute the script in the command line. unless the script is in your path you will need to choose how to execute it. by prefixing with ./ or using the full path.
Using positional parameters
- a variable name with a single numeral, such as $1, $2, or $3. the $ symbol is how you access and expand the variable on the command line. positional parameters are typically number from 1 to 9 and begin after in option from the program.
EX:
#!/bin/bash
# filename:userscript
# create a user, username read from $1, shell from $2
useradd -m -s /bin/$2 $1
./userscript wcarroll sh
Testing for conditions
the test command is useful for comparing the dates of files, whether something is newer or older then another, or even that a file exists and is part of a certain type. to test whether an object exists on the file system the -f option is used followed by the full path and name of the object being tested.
Having Users make variables
#!/bin/bash
# this script will ask the user for their first and last name
# Then it echoes back their full name
echo "What is your first name?"
read fname
echo "What is your last name?"
read lname
echo "Your name is $fname $lname"
Script Constructs
if/else statements
a if/else statement is a simple tool which essentially tests a condition and performs an action if that condition is true.
EX:
#!/bin/bash
if [-d "mosso"]
then
echo "the mosso directory exists."
else
echo "the mosso directory does not exists."
fi
The for Loop
- used to do a task a specific number of times, or for as many times as it's found in a location. for loops start with the keyword for and typically includes a range or found number of items.
EX:
if you want all the files in the /home directory and subdirectories to be rename from file to file.bak
for X in 'find /home -type f'
do
mv $X $X.bak
done
While and until statements
a while or until statement performs a particular task while a value is less than or greater than a certain value or until the incrementing value reaches a certain threshold; then it exits. typically a while or until statement includes an action that increments or decrements a counter until the threshold or exit value is reacheed.
EX:
#!/bin/bash
value=0
last=20
while [$value -lt $last]
do
echo $value
value='expr $value+1'
done
you can see, the loop uses the $value variable to store the value that will be increased in the do section. while this value is still less then 20, the loop continues. you'll find that it starts at a value of 0 and goes to 20; if you want it to start at 1 and go to 20, make value=1
EX:
#!/bin/bash
value=0
last=20
while [$value -ge $upper]
do
echo $value
value='expr $value+1'
done
this loop is similar to previous while loop. it simply declares a value, declares an upper limit to reach and then starts echoing its progress to the screen until it has done the task 20 times, or from 0 to 20.
Last updated
Was this helpful?