1 | #!/bin/bash |
---|
2 | # Selecting shell |
---|
3 | |
---|
4 | # Numeric if # Comment |
---|
5 | value=-4 # Only integers |
---|
6 | large=false |
---|
7 | if test ${value} -eq 1; then # `test' evaluation of condition |
---|
8 | echo "one" |
---|
9 | elif test ${value} -lt 0; then |
---|
10 | echo "Negative" |
---|
11 | else |
---|
12 | echo "Large" |
---|
13 | large=true |
---|
14 | fi |
---|
15 | |
---|
16 | # Boolean variable |
---|
17 | if ${large}; then |
---|
18 | echo "Is large!" |
---|
19 | exit # To stop every thing |
---|
20 | fi |
---|
21 | |
---|
22 | # String if |
---|
23 | value="one" |
---|
24 | if test ! ${value} = 'one'; then # '!' as the contrary |
---|
25 | echo "Different than 'one'!" |
---|
26 | exit |
---|
27 | fi |
---|
28 | |
---|
29 | # File existence |
---|
30 | dateval=`date +%Y%m%d%H%M%S` # Saving into a variable the result of `date' coreutil date tool |
---|
31 | filen=${dateval}'_file.txt' |
---|
32 | if test -f ${filen}; then # '!' as the contrary |
---|
33 | echo "File '"${filen}"' does not exist!" |
---|
34 | exit |
---|
35 | fi |
---|
36 | |
---|
37 | # Incremental loop # Comment |
---|
38 | i=1 # No spaces on definition of variables |
---|
39 | Rangeloop=10 # Case-sensitive! |
---|
40 | echo "Initial values" # Printing on the terminal |
---|
41 | while test ${i} -le $Rangeloop; do # Loop initialization ';' for new line |
---|
42 | echo ${i} |
---|
43 | i=`expr ${i} + 1` # Loop increment using coreutil `expr' |
---|
44 | done |
---|
45 | |
---|
46 | # Incremental loop # Comment |
---|
47 | i=1 # No spaces on definition of variables |
---|
48 | Rangeloop=10 # Case-sensitive! |
---|
49 | files='' |
---|
50 | echo "Initial values" # Printing on the terminal |
---|
51 | while 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' |
---|
62 | done |
---|
63 | |
---|
64 | echo "quadratic values" |
---|
65 | # 'variable' loop |
---|
66 | for 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} |
---|
70 | done |
---|
71 | |
---|
72 | echo "Content of files" |
---|
73 | # 'assigned variables' loop |
---|
74 | fs=`echo ${files} | tr ':' ' '` # Use of `tr' coreutil substitution tool |
---|
75 | for filen in ${fs}; do |
---|
76 | echo ${filen}"..." |
---|
77 | cat ${filen} |
---|
78 | done |
---|
79 | |
---|
80 | # Pipes |
---|
81 | Nfiles=`ls -1 *_file.txt | wc -l` # '|' to connect consecutively instrucions |
---|
82 | echo "Number of files:"$Nfiles |
---|
83 | |
---|
84 | # Case |
---|
85 | num=3 |
---|
86 | case ${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 | ;; |
---|
99 | esac |
---|
100 | |
---|
101 | # Complet file name generation |
---|
102 | Syear=2012 |
---|
103 | Eyear=2014 |
---|
104 | iyr=${Syear} |
---|
105 | otex=table |
---|
106 | cat << 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 |
---|
113 | EOF |
---|
114 | while 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 \\\\ |
---|
130 | EOF |
---|
131 | im=`expr ${im} + 1` |
---|
132 | done |
---|
133 | iyr=`expr ${iyr} + 1` |
---|
134 | done |
---|
135 | cat << EOF >> ${otex}.tex |
---|
136 | \\end{tabular} |
---|
137 | \\end{center} |
---|
138 | |
---|
139 | \\end{document} |
---|
140 | EOF |
---|
141 | pdflatex ${otex} |
---|
142 | pdflatex ${otex} |
---|
143 | evince ${otex}.pdf & |
---|
144 | |
---|
145 | # Function |
---|
146 | function 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 | |
---|
158 | foldInf ./ |
---|