Linker IT Software
Google
Web www.oraxcel.com
menubar-top-links menubar-top-rechts
Home Help Search Login
Welcome, Guest. Please Login.
SQL*XL: Database to Excel bridge litLIB: Excel power functions pack ExcelLock: Locking and securing your valuable Excel spreadsheets encOffice: Protect your Excel file easy and safe encOffice: Protect your Excel file easy and safe
Pages: 1
bash shell scripting (Read 8316 times)
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
bash shell scripting
22.06.09 at 07:56:31
 
bash shell scripting
 
In this topic I will collect some bash shell scripting examples.
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Re: bash shell scripting
Reply #1 - 22.06.09 at 08:00:52
 
bash for loop
 
This will loop the variable A from 0 to 180 in steps of 10.
 
for ((A=0 ; A<=180 ; A+=10))
do
  <<statements>>
done
 
for i in {0..9}; do cp morph.becke.vwn ../0.$i; done;
Back to top
 
« Last Edit: 24.01.11 at 10:22:35 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
bash arithmatic
Reply #2 - 22.06.09 at 08:51:00
 
bash arithmatic
 
To do arithmatic in the bash shell you need to use the $(( expression )) syntax. For example to define a variable B as B=180-A you code:
 
B= $(( 180-$A ))
 
To do arithmatic with decimals the bc program needs to be used. Using the syntax above only integer arithmatic can be done.
 
Example of floating point arithmatic with the bc program With scale the number of decimals is specified:
Code:
STEP=5
I=$( echo "scale=5;$STEP/10 "|bc)

cat<<EOF>myfile.d12
Title line
EXTERNAL
ATOMDISP
2
1     $( echo "scale=5;$I*     0.039	"|bc) $( echo "scale=5;$I*    -0.0394	"|bc) $( echo "scale=5;$I*    0.008	"|bc)
2     $( echo "scale=5;$I*     -0.039     "|bc) $( echo "scale=5;$I*     0.0394	"|bc) $( echo "scale=5;$I*    -0.008     "|bc)
...
EOF
 

Back to top
 
« Last Edit: 26.05.12 at 10:02:37 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
bash if statement
Reply #3 - 22.06.09 at 08:53:56
 
bash if statement
 
The if statement in the bash shell has a somewhat peculiar syntax I find in the sense that the expression to evaluate is ended with a semi colon.
 
An if statement to find if A mod 10 is unequal to one is coded as:
 
if [ $$( $A%10 )) != 0 ]; then
  << statements >>
fi
 
Please note the spaces. It is necessary to type a space behind the 0 and the 10 and a space between the )) and !=
 
Example: testing for empty string
 
if [-n $1]; then
  echo "First parameter is empty"
else
  echo "First parameter is $1"
fi
Back to top
 
« Last Edit: 07.11.10 at 12:03:35 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
concatenating files
Reply #4 - 23.02.10 at 19:59:32
 
concatenating files
 
I have 10x10 = 100 files that need to be sandwiched between a header and a trailer (tmp). File 0-0 will be created from head, then 0-0.coords and tmp. The next file 0-10 will be created from head, 0-10.coords and tmp. And so on...
 
Code:

for ((A=0 ; A<=90 ; A+=10))
do
for ((B=0 ; B<=90 ; B+=10))
do
  cat head coords/$A-$B.coords tmp > $A-$B
done
done
 

Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
looping over a set of files
Reply #5 - 23.02.10 at 20:59:50
 
looping over a set of files
 
To loop over a set of files using the bash script the following commands can be used.  
 
for f in $(find . -name "*.out" -print); do
  echo $f;
done;
 
Another way to do this is using the ls command and using back quotes:
for i in `ls *.out`; do
  echo $i;
done;
 
It is also possible to do this on one line:
for i in `ls *.job`; do myscript $i; done;
Back to top
 
« Last Edit: 06.11.10 at 20:50:24 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
store command output into a variable
Reply #6 - 23.02.10 at 21:14:36
 
store command output into a variable
 
To store the output of a command in a variable you can use the following syntax. Note that the sed command extracts all lines between 2 indicated text strings. Then the line matching TOTAL DRF is grepped out.
 
a=`sed -n "/DRF ENERGIES analysis group  2/,/DRF ENERGIES/p" myfile.out | grep "TOTAL DRF"`;
echo $a;
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
bash substring
Reply #7 - 23.02.10 at 21:55:24
 
bash substring
 
Extracting a substring from a string variable:
 
The following command prints the string skipping the first 2 characters:
a="Hello"
echo ${a:2}
 
Take 10 characters from string a starting at position 15:
a="The fox jumps over the box"
echo ${a:15:10}
Back to top
 
« Last Edit: 07.11.10 at 11:39:30 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
looping whole lines in a file
Reply #8 - 06.11.13 at 00:08:07
 
looping whole lines in a file
 
To loop over all whole lines in a file:
while read line; do echo $line; done < my_file
 
To loop all words in a file:
for w in `cat my_file`; do echo $w; done;
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Pages: 1