-
Notifications
You must be signed in to change notification settings - Fork 2
/
course.sh
137 lines (110 loc) · 2.01 KB
/
course.sh
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
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
#Comment
echo "hello World"
#------------------
./hello_world.sh
#------------------
chmod 755 hello_world #make an executable
#7 --> read, write, execute
#5 --> read, execute
#6 --> read, write
#4 --> read only
#3 --> write, execute
#2 --> write only
#1 --> execute only
#0 --> Can't do anything to the file
#Variable creation -------------------------
myName = "Ace"
declare -r NUM1=5 #constant
num2=4
num3=$((NUM1+num2))
num4=$((NUM1-num2))
num5=$((NUM1*num2))
num6=$((NUM1/num2))
echo "5 + 4 = $num3"
echo "5 - 4 = $num4"
echo "5 * 4 = $num5"
echo "5 / 4 = $num6"
echo((5**2)) #exponents
echo((5%4)) #modulo
i+=2
i-=2
i*=2
i/=2
#creating a random variable
rand=5
let rand+=4
echo "$rand"
#using python for floating points
num7=1.2
num8=3.4
num9=$(python -c "print $num7+$num8")
echo $num9
#print to screen
cat<< END #choose keyword for ending printing section
this text ends here
END
#functions on bash script----------------------
getDate(){
date
return
}
#calling function
getDate
#global variable
name="Arturo"
#local variable
demLocal(){
local name="Art"
return
}
demLocal
echo $name
getSum(){
local num3=$1
local num4=$2
local sum=$((num3+num4))
echo $sum
}
num1=5
num2=6
sum=$(getSum num1 num2)
echo $sum
#Conditionals on bash script------------------
read -p "How old are you" age
#greater than or equal to 16
if [$age -ge 16]
then
echo "you can drive"
elif [$age -eq 15]
echo "You can drive next year"
else
echo "You can´t drive"
fi
#alternative
if ((num==10)); then
echo "Your number equals 10"
fi
if ((num > 10)); then
echo "It is greater then 10"
else
echo "It is less then 10"
fi
if (( ((num % 2)) == 0 )); then
echo "It is even"
fi
if (( ((num > 0)) && ((num<11)) )); then
echo "Num is between 1 and 10"
fi
#creating a file
#create file and open it
touch sample_file && vim sample_file
#if file does't exist make it
[ -d sample_dir ] || mkdir samp_dir
#Strings on bash scripting------------------------
str1=""
str2="Sad"
str3="Happy"
if ["$str1"]; then
echo "$str1 is not null"
fi