Doc/Tools/External: test.bash

File test.bash, 4.0 KB (added by acosce, 9 years ago)

fichier TP bash BootCamp? 24 mars 2016

Line 
1#!/bin/bash
2# Selecting shell
3
4# Numeric if                            # Comment
5value=-4                                # Only integers
6large=false
7if test ${value} -eq 1; then            # `test' evaluation of condition
8  echo "one"
9elif test ${value} -lt 0; then
10  echo "Negative"
11else
12  echo "Large"
13  large=true
14fi
15
16# Boolean variable
17if ${large}; then         
18  echo "Is large!"
19  exit                                  # To stop every thing
20fi
21
22# String if
23value="one"
24if test ! ${value} = 'one'; then        # '!' as the contrary
25  echo "Different than 'one'!"
26  exit
27fi
28
29# File existence
30dateval=`date +%Y%m%d%H%M%S`            # Saving into a variable the result of `date' coreutil date tool
31filen=${dateval}'_file.txt'
32if test -f ${filen}; then             # '!' as the contrary
33  echo "File '"${filen}"' does not exist!"
34  exit
35fi
36
37# Incremental loop                      # Comment
38i=1                                     # No spaces on definition of variables
39Rangeloop=10                            # Case-sensitive!
40echo "Initial values"                   # Printing on the terminal
41while test ${i} -le $Rangeloop; do      # Loop initialization ';' for new line
42  echo ${i}
43  i=`expr ${i} + 1`                     # Loop increment using coreutil `expr'
44done
45
46# Incremental loop                      # Comment
47i=1                                     # No spaces on definition of variables
48Rangeloop=10                            # Case-sensitive!
49files=''
50echo "Initial values"                   # Printing on the terminal
51while test ${i} -le $Rangeloop; do      # Loop initialization ';' for new line
52  echo ${i}
53  iS=`printf %02d ${i}`                 # `printf': coreutil format printing tool
54  filen=${iS}_file.txt
55  echo ${i} > ${filen}                  # '>' to write the left side result into a file
56  if test ${i} -eq 0; then              # Staring an if
57    files=${filen}
58  else
59    files=${files}':'${filen}
60  fi
61  i=`expr ${i} + 1`                     # Loop increment using coreutil `expr'
62done
63
64echo "quadratic values"
65# 'variable' loop
66for filen in *_file.txt; do             # Getting all files with the given expression
67  val=`cat ${filen}`                    # Outputting content of file and keeping it into a variable
68  valpot=`expr ${val} '*' ${val}`
69  echo ${valpot}
70done
71
72echo "Content of files"
73# 'assigned variables' loop
74fs=`echo ${files} | tr ':' ' '`         # Use of `tr' coreutil substitution tool
75for filen in ${fs}; do
76  echo ${filen}"..."
77  cat ${filen}
78done
79
80# Pipes
81Nfiles=`ls -1 *_file.txt | wc -l`       # '|' to connect consecutively instrucions
82echo "Number of files:"$Nfiles
83
84# Case
85num=3
86case ${num} in
87  1)
88    echo "one"
89  ;;
90  2)
91    echo "two"
92  ;;
93  3)
94    echo "three"
95  ;;
96  *)
97    echo "other than one, two, three !"
98  ;;
99esac
100
101# Complet file name generation
102Syear=2012
103Eyear=2014
104iyr=${Syear}
105otex=table
106cat << EOF > ${otex}.tex
107\\documentclass{article}
108
109\\begin{document}
110\\begin{center}
111\\begin{tabular}{cccl}
112{\\bfseries{year}} & {\\bfseries{month}} & {\\bfseries{Ndays}} & {\\bfseries{file}}\\\\ \\hline
113EOF
114while test ${iyr} -le ${Eyear}; do
115  im=1
116  while test ${im} -le 12; do
117    imS=`printf %02d ${im}`
118    d1=`date +%j -d"${iyr}${imS}01"`
119    d2=`date +%j -d"${iyr}${imS}01 1 month"`
120    Ndays=`expr ${d2} - ${d1}`
121    if test ${d2} -eq 1; then Ndays=31; fi
122    id=1
123    while test ${id} -le ${Ndays}; do
124      idS=`printf %02d ${id}`
125      id=`expr ${id} + 1`
126    done
127    mon=`date +%b -d"${iyr}${imS}${idS}"`
128    cat << EOF >> ${otex}.tex
129${iyr} & ${mon} & ${Ndays} & ${iyr}${imS}${idS}.nc \\\\
130EOF
131    im=`expr ${im} + 1`
132  done
133  iyr=`expr ${iyr} + 1`
134done
135cat << EOF >> ${otex}.tex
136\\end{tabular}
137\\end{center}
138
139\\end{document}
140EOF
141pdflatex ${otex}
142pdflatex ${otex}
143evince ${otex}.pdf &
144
145# Function
146function foldInf() {
147# Function to provide information from a folder
148  fold=$1
149
150  Nfiles=`ls -1 ${fold} | wc -l`
151  DiskSpace=`du -hsc ${fold} | grep total`
152
153  echo "Information of '"${fold}"' _______"
154  echo "Nfiles: "${Nfiles}
155  echo "Disk space: "${DiskSpace}
156}
157
158foldInf ./