Tuesday, January 1, 2013

For Loop in Shell Script

The below code snippet shows how to use for loop in shell script .

List="one two three"
for a in $List     # Splits the variable in parts at whitespace.
do
  echo "$a"
done
# one
# two
# three
echo "---"
for a in "$List"   # Preserves whitespace in a single variable.
do #     ^     ^
  echo "$a"
done
# one two three
echo "---"
for file in `ls * 2>/dev/null`
do
 echo "File name : $file"
done


OUTPUT :

-bash-3.2$ sh for-loop-ex
one
two
three
---
one two three
---
File name : arithmatic_sh
File name : case_example
File name : check_min_params
File name : comparion_op_ex
File name : for-loop-ex
File name : passing_parameter_mech
File name : simple_function
File name : simple_multiplication
Explanation :

ls * 2>/dev/null  
                         it will try to list all the files in current directory if there is no files found in current directory means it surpress the warning message
/dev/null            this is the 0 device file

No comments:

Post a Comment

Price List Query for Item

 SELECT qph.list_header_id,        qph.name,        qph.description,        qphh.start_date_active,        qphh.currency_code,        q...