-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeptember_24.sh
143 lines (101 loc) · 2.44 KB
/
September_24.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
138
139
# September 24
# Shell Scripts -------------------------------------
# files that contain sequences of Linux commands, executed as programs
# Special Variables ---------------------------------
# accept arguments from the command line
$0, $1, $2, $3
$# # number of arguments, excluding $0
$@ # all arguments (except $0) as separate strings
# Return codes --------------------------------------
# every program returns a status code when finished
# in Unix, 0 is success, non-zero is failure
$? # status of most recently executed command
# if: semantics -------------------------------------
# if execute commands once if the test commands succeed (return code is 0)
if test-commands; then
commands
[elif more-test-commands; then more-commands]
[else alternate-commands]
fi
# while: semantics ---------------------------------
while test-commands;
do
commands
done
# for: semantics -----------------------------------
for name in [list];
do
commands
done
# Arithmetic with shell variables ------------------
$(( express )) # to evaluate arithmetic expressions
# Bash Procedures ----------------------------------
function function_name { # equivalent forms
commands
}
function_name () {
commands
}
# Examples ------------------------------------------
#!/bin/bash
echo "Script name: ${0}"
echo "First arg: ${1}"
echo "Num of args: ${#}"
echo "All args: ${@}"
n=4
echo "Fourth arg: ${!n}" # outputs the argument 4
echo "Fourth arg: ${!4}" # undefined
#!/bin/bash
#function "usage"
usage () {
echo "Usage: $0 password"
}
# check the number of arguments greater than 1
if [ $# -ne 1 ]; then
usage
exit 1
fi
# try to match words in a dictionary
egrep "^$1$" /usr/share/dict/words > /dev/null
# output message
if [ $? -eq 0 ]; then
echo Not a good password
else
echo Maybe a good password
fi
#!/bin/bash
# list of files with .cpp
for name in *.cpp; do
mv ${name} ${name%cpp}cc
done
#!/bin/bash
x=0
for word in `cat $2`; do
if [ $word = $1 ]; then
x=$((x+1))
fi
done
echo $x
#!/bin/bash
answer () {
if [ $1 -eq 31 ]; then
echo "This month: on the ${1}st."
else
echo "This month: on the ${1}th."
fi
}
answer `cal | awk '{print $6}' | grep "[0-9]" | tail -1`
#!/bin/bash
answer () {
if [$2]; then
preamble=$2
else
preamble="This month"
fi
if [$1 -eq 31]; then
echo "${preamble}: on the ${1}st."
else
echo "${preamble}: on the ${1}th."
fi
}
answer `cal $1 $2 | awk '{print $6}' | grep "[0-9]" | tail -1` $1