Showing posts with label BASH. Show all posts
Showing posts with label BASH. Show all posts

Sunday, 23 March 2014

BASH LOOPS


Loop is to repeat some code continuously and exit based on the some expression evaluation.

FOR LOOP

basic for loop struct:
for statement
do
       commands block
done

there are quite a few common examples

  • for variable in 1 2 3 4 5
  • for variable in {1..5}
  • for i in $(seq 1 2 100)
  • for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
  • for argument in "$*"
  • for(( integer = 1; integer <= 5; integer++ ))
  • for(( a=1, b=5; a <= 5; a++, b-- ))


WHILE LOOP

while expression
do
       commands block
done
the statement will be evaluated if true then will do the block, and re-evaluate the expression. The loop will exit untile the expression is false.
unitl loop is quite similar but until will always execute the loop at least once then evaluate the expression

break exit the loop
continue ignore the left loop block and continue an new loop

SELECT LOOP

select is very similar to the case statement in bash, it provides an interactive user interface.
Here is the sample code for user to select from a specified list

echo "What is your favorite color? "

select color in "red" "blue" "green" "white" "black"
do
    if [ $color ] ; then
              echo "You have selected $color"
              break
       else
              echo "Invalid selection"
       fi
done

it will ask the user to key in a
such as
what is your favorite color?
1)      red
2)      blue
3)      green
4)      white
5)      black
#? Red
Invalid selection
#? 0
Invalid selection
#? 1

You have selected red


Monday, 24 February 2014

BASH IO redirection

IO redirection:
IO redirection will capture a file , commands, program, scripts’s output, and send it to another file, commands and scripts.

Background: every script has three standard file discriptor:
       stdin: standard input
       stout: standard output
       stderr: standard error output

IO redirection example:
Output redirection
> file: output to the target file, it will overwrite the file content.
>> file: output to the target file, it will append the content at the end of the file
>| file : override the file even with noclobber option

stderr redirection
2>newfile: redirect the stderr to the newfile, for example: ls –al zz 2>newfile.
              If no file matched. The error msg will be written to newfile

stdin redirection
script < file: use file as my stdin instead of default stdin

<<delimiter – redefine the delimiter
For example
#cat > mytest<<GO
>this is my test
>it is ok
>let’s GO
>GO
#cat mytest
this is my test
it is ok
let’s GO

block redirction

> outputfile and < input file. It can used to change the stdin or stdout temporarry. 

Saturday, 22 February 2014

BASH variables

bash variables are used to store the useful information referred by the scripts

  1. local variables: declared and used only in the local shall process.
  2. environment variables: used by the login process and the sub process. the environment variables can be used by all editor's, scripts
  3. parameter variables: used to pass the parameters to shell scripts. they are read only.

some examples:
variable=value or ${variable=value} #how to set value to a variable:
echo $variable         #get the value
unset $variable                 #clear the variable
readonly variable #set the variable to readonly (immutable), you need to set the value and set the property to readonly
let a=a+1 # integer operations

by default, the BASH variable is string. default value is null if not declared before. if you have already set a variable to a string, you can still use the variable for integer operations. the variable will be 0 as initial value.

environment variables:

eg: export environment-variables # declare it is a environment variable
you can use env command to show the defined environment variables. To set and unset variables are the same as

there are some important pre-defined environment variables for the users
PWD,OLDPWD: current user location and previous location
PATH: the location shell is going to search for external commands, scripts and executable program
HOME: user's home directory
SHELL: user's default shell (/bin/bash)
USER: user login name such as root
UID: user UID.
PPID: parent PID.
some tips: the child process can inherit the environment variable from parenet process but if the variable is changed in child process, it can't be passed to the parent

~/.bash_profiles: usually where you define your BASH environment variables.
if .bash_profiles is not existing, it will use /etc/profiles as alternative file.
~/.login is used by Cshell, ~./profiles is used by kshell. the variables there can be referred by BASH but strongly not recommended.

parameter variables:


  • $0: the script itself, $1, $2, $3: the first, second and third parameters. ${10}, the 10th parameter
  • $# paramter numbers
  • $* all parameters
  • $? exit code. 0 for successful and 
  • $$ current PID

Quotes in Bash


"" (partially quote): all the characters are treated as normal characters except $ ` and \ . it can also reserve the space in the variable
'' (full quote): all the characters are treated as normal characters.
`command`: use the command as a linux command

Sunday, 16 February 2014

BASH text file processing

In this Blog, we will show some common bash commands about text file processing.

cut: cut is a powerful tool to extract the dedicated colume, fields tool from a text-based file. 
The common options are:
  • -c <list>:      the specified columns for output.
  • -d <delimiter>  the delimiter used to separate the file, default is space and tab
  • -f <fields>     the fields for output.


For example,
The command to print the 1st and 7th field of the /etc/passwd file using : as delimiter

The command to print the 1st to 10th characters of /etc/passwd


sort: display the file by sorting the field
Some important paramters
  • -b: ignore the blank
  • -d: sort by dictionary
  • -g: sort by float
  • -f: ignore the case
  • -k: define the key
  • -n: sort by integer
  • -o: send the output to output file
  • -t: delimiter
  • -u: unique


Example: sort the file by the second field as float


Sort the /etc/password file using UID by descend.
sort -t : -k3 -n -r /etc/passwd


uniq: delete the duplicated records
-c: show the line number
-i: ignore the case
-u: only show the unduplicated records
-d: only show the duplicated records



wc command:
show the files, line counts, word counts and character counts
the file has to use space or tab as delimiters


head and tail commands to show the first and list lines (by default is 10)
head –n number <file>:  the first number lines
head –n -number <file> all the lines to the last number-st line
tail –n number <file>: the last number lines

tail –n +number <file>: the bottom number lines