-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_script_some_more_tips.txt
128 lines (89 loc) · 1.96 KB
/
bash_script_some_more_tips.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
BASH SHELL SCRIPTS TIPS:
#!/bin/bash -options (-x -i)
MATH:
var1=100;var2=200;var3=$(expr $var1 + $var2);echo "Total: $var3"
Total: 300
var1=100;var2=200;var3=$[$var1 + $var2];echo "Total: $var3"
Total: 300
VAR:
var_teste=1
var_year=$(date +%Y)
var1=$var2
If condition true/false or command (exit 0)
Then
:
Else
Fi
If condition/command
Then
:
Elif condition/command
Then
:
Elif condition/command
Then
:
Else
:
Fi
Comparisons:
String:
[ x -eq y ] : x=y
[ x -ge y ] : x>=y
[ x -gty ] : x>y
[ x -le y ] : x<=y
[ x -lt y ] : x<y
[ x -ne y ] : x<>t
Numeric:
[ x = y ] : x=y
[ x !=y ] : x<>y
[ x < t] : x<y
[ x > y ] : x>y
[ -n y ] : not zero
[ -z y] : zero
File teste:
If [ -d $file ]
Then
-d file = exist and dir
-e file = exist
-f file = exist and file
-r file = exist and perm read
-s file = exist and not empty
-w file = exist andperm write
-x file = exist and perm exec
-O file = exist and owned by user
-G file = exist and owned by group
file1 -nt file2 = file1 newer file2
file1 -ot file2 = file1 older file2
Conditional Compound:
[ condition ] && [ condition ] = AND
[ condition ] || [ condition ] = OR
Test var zero or null:
If [ -n $VAR ] # var is not null
If [ -z $VAR ] # var is empty
Looping Specific Commands:
while/until
do
break (get out single looping). One break is needed for each nested looping
continue (skip commands above and go back do do
done
Linux Shell Scripts: for
for i in *; do qemu-img info "$i"; done
for i in *.txt; do echo "hello $i"; done
for i in teste* ; do cp $i /tmp/ ; done
while read i; do echo $i; done < teste.txt
for i in $(cat peptides.txt); do echo $I; done
sort -d teste.txt | uniq -d
cp -i alex.txt ../teste2/ <<< "n
for z in *.zip; do cp -i $z /userdata/roms/neogeo <<< "n; done
for word in $(cat peptides.txt); do echo $word; done
i=0
while [ $i -lt 10 ]; do echo "valor: $i" ; i=$[$i+1]; done;
while read -r line;
do
echo "$line" ;
done < input.file
cat /etc/passwd | while read LREAD
do
echo ${LREAD}
done