bash script
1) shell scripting
2) how to create and use variables
3) Testing and decision making
4) command line argument processing
5) input and output, include user input
6) if statements
7) exit statuses
8) functions
9)
10)
11)
12)
13)
#!/bin/bash
#comment
echo "Hello World!"
myName="name" #variable
#all variable are trited as a string
# no space on either side of =
#declar a constant
declare -r NUM1=5
num2=4
num3=$((NUM1+num2))
num3=$((NUM1-num2))
num3=$((NUM1*num2))
num3=$((NUM1/num2))
echo "5 + 4 = $num3"
echo $((5**2))
echo $(( 5%4 ))
i += 2
i + i =2
rand=5
let rand+=4
echo "$rand"
echo"rand++ = $(( rand ++ ))"
echo"++rand = $(( ++ rand ))"
echo"rand-- = $(( rand -- ))"
echo"--rand = $(( --rand ))"
num7=1.2
num8=3.4
num9=$(pythin - c "print $num7+$num8")
echo $num9
cat<< END
This text
prints
on many lines
END
#Functions
getDate(){
date
return
}
getDate
name="name" #global var
demLocal(){
local name="Paul" #local var
return
}
demLocal
echo "$name"
getSum(){
local num=$1
local num=$2
local sum=$((num3+num4))
echo $sum
}
num1=5
num2=6
sum=$(getSum num1 num2)
echo "the sum is $sum"
#conditionals
read -p "What is your name? " name
echo "Hello $name"
read -p "How old are you? " age
if [$age -ge 16] #another options eq ne le lt ge gt
then
echo "You can drive"
elif [$age -eq 15]
then
echo "You can drive next year"
else
echo "You can't drive"
fi
read -p "Enter a number : " num
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 ten"
fi
if (( (( num % 2)) == 0)); then
echo "It is even"
fi
#Logical operators
if (( (( num > 0)) && ((num <11)) ));
then
echo "$num is between 1 and 10"
fi
touch samp_file && vim samp_file
[ -d samp_dir] || mkdir samp_dir
str1=""
str2="Sad"
str3="Happy"
if [ "$str1" ]; then
echo "$str1 is not null"
fi
if [ -z "$str1" ]; then
echo "str1 has not value"
fi
if ["$str2" === "$str3" ]; then
echo "$str2 equals $str3"
elfi [ "$str2" != "$str3" ] ; then
echo "$str2 is not equal to @str3"
fi
if ["$str2" > "$str3" ]; then
echo "$str2 is greater then $str3"
elif [ "$str2" < "$str3"]; then
echo "$str2 is less then $str3"
fi
# files
file1=".test_file1"
file2="./test_file2"
if [ - e "$file1"]; then
echo "$file1 exists"
if [-f "$file1"]; then
echo "$file1 is a normal file"
fi
if [ -r "$file1" ]; then
echo "$file1" is readable"
fi
if [ -w "$file1" ]; then
echo "$file1 is writable"
fi
if [ -e "$file1" ]; then
echo "$file1 is executable"
fi
if [ -d "$file1" ]; then
echo "$file1 is a directory"
fi
if [ -L "$file1" ]; then
echo "$file1 is a symbolic link"
fi
if [ -p "$file1" ]; then
echo "$file1 is named pipe"
fi
if [ -S "$file1" ]; then
echo "$file1 is a network sockeet"
fi
if [ -G "$file1" ]; then
echo "$file1 is owned by the group"
fi
if [ -O "$file1" ]; then
echo "$file1 is owned by the userid"
fi
fi
# Reg exp
#Extendet test
read -p "Validate Date: " date
pat="^[0-9]{8}$"
if [[ $date =~ $pat ]] ; then
echo "$date is valid"
else
echo "$date not valid"
fi
read -p "Enter 2 Numbers to Sum : " num1 num2
sum=$((num1+num2))
echo "$num1 + $num2 = $sum"
read -sp "enter the secret code" secret
if [ $secret" == "password" ]; then
echo"Enter"
else
echo "Wrong Password"
fi
#Parametr expation
OIFS="$IFS"
IFS=","
read -p "Enter 2 numbers to add separated by a comma" num1 num2
num1=${num1//[[:blank:]]/}
num2=${num1//[[:blank:]]/}
sum=$((num1+num2))
sum "$num1 + $num2 = $sum"
IFS="$OIFS"
name="name"
echo "${name}'s toy"
samp_string ="Thr dog climed the tree"
echo "${samp_string//dog/cat}"
echo "I am ${name:=name}"
#case
read -p "How old are you: " age
case $age in
[0-4])
echo "To young for school"
;;
5)
echo "Go to Kindergarten"
;;
[6-9]|1[0-8])
grade=$((age-5))
echo "Go to grade $grade"
;;
*)
echo "You are to old for school"
;;
esac
can_vote= 0
age=18
((age >=18?(can_vote=1):(can_vote=0)))
echo "Can Vote : $can_vote"
rand_str="A randomstrging"
echo "String Length : ${#rand_str}"
echo "${rand_str:2}"
echo "${rand_str:2:7}"
2)Indirect expansion
3)
4)
Checking and assigning using
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20) what to use in the script insted of "if" - Best Practices
a) exit codes
; - to chain commands together
echo "sth" ; echo "sth2"
b) logical operators
&& - "and and" only runs second command if first succeed
cat file && echo "sth2"
|| - "or or" it runs second command only if first one not succeed
cat file || echo "sth2"
& - |"and" it runs both of command one after another without wait for first command to end running
sleep 5 & echo "sth2"
21)
100)
a)https://www.youtube.com/watch?v=hwrnmQumtPw
2) how to create and use variables
3) Testing and decision making
4) command line argument processing
5) input and output, include user input
6) if statements
7) exit statuses
8) functions
9)
10)
11)
12)
13)
#!/bin/bash
#comment
echo "Hello World!"
myName="name" #variable
#all variable are trited as a string
# no space on either side of =
#declar a constant
declare -r NUM1=5
num2=4
num3=$((NUM1+num2))
num3=$((NUM1-num2))
num3=$((NUM1*num2))
num3=$((NUM1/num2))
echo "5 + 4 = $num3"
echo $((5**2))
echo $(( 5%4 ))
i += 2
i + i =2
rand=5
let rand+=4
echo "$rand"
echo"rand++ = $(( rand ++ ))"
echo"++rand = $(( ++ rand ))"
echo"rand-- = $(( rand -- ))"
echo"--rand = $(( --rand ))"
num7=1.2
num8=3.4
num9=$(pythin - c "print $num7+$num8")
echo $num9
cat<< END
This text
prints
on many lines
END
#Functions
getDate(){
date
return
}
getDate
name="name" #global var
demLocal(){
local name="Paul" #local var
return
}
demLocal
echo "$name"
getSum(){
local num=$1
local num=$2
local sum=$((num3+num4))
echo $sum
}
num1=5
num2=6
sum=$(getSum num1 num2)
echo "the sum is $sum"
#conditionals
read -p "What is your name? " name
echo "Hello $name"
read -p "How old are you? " age
if [$age -ge 16] #another options eq ne le lt ge gt
then
echo "You can drive"
elif [$age -eq 15]
then
echo "You can drive next year"
else
echo "You can't drive"
fi
read -p "Enter a number : " num
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 ten"
fi
if (( (( num % 2)) == 0)); then
echo "It is even"
fi
#Logical operators
if (( (( num > 0)) && ((num <11)) ));
then
echo "$num is between 1 and 10"
fi
touch samp_file && vim samp_file
[ -d samp_dir] || mkdir samp_dir
str1=""
str2="Sad"
str3="Happy"
if [ "$str1" ]; then
echo "$str1 is not null"
fi
if [ -z "$str1" ]; then
echo "str1 has not value"
fi
if ["$str2" === "$str3" ]; then
echo "$str2 equals $str3"
elfi [ "$str2" != "$str3" ] ; then
echo "$str2 is not equal to @str3"
fi
if ["$str2" > "$str3" ]; then
echo "$str2 is greater then $str3"
elif [ "$str2" < "$str3"]; then
echo "$str2 is less then $str3"
fi
# files
file1=".test_file1"
file2="./test_file2"
if [ - e "$file1"]; then
echo "$file1 exists"
if [-f "$file1"]; then
echo "$file1 is a normal file"
fi
if [ -r "$file1" ]; then
echo "$file1" is readable"
fi
if [ -w "$file1" ]; then
echo "$file1 is writable"
fi
if [ -e "$file1" ]; then
echo "$file1 is executable"
fi
if [ -d "$file1" ]; then
echo "$file1 is a directory"
fi
if [ -L "$file1" ]; then
echo "$file1 is a symbolic link"
fi
if [ -p "$file1" ]; then
echo "$file1 is named pipe"
fi
if [ -S "$file1" ]; then
echo "$file1 is a network sockeet"
fi
if [ -G "$file1" ]; then
echo "$file1 is owned by the group"
fi
if [ -O "$file1" ]; then
echo "$file1 is owned by the userid"
fi
fi
# Reg exp
#Extendet test
read -p "Validate Date: " date
pat="^[0-9]{8}$"
if [[ $date =~ $pat ]] ; then
echo "$date is valid"
else
echo "$date not valid"
fi
read -p "Enter 2 Numbers to Sum : " num1 num2
sum=$((num1+num2))
echo "$num1 + $num2 = $sum"
read -sp "enter the secret code" secret
if [ $secret" == "password" ]; then
echo"Enter"
else
echo "Wrong Password"
fi
#Parametr expation
OIFS="$IFS"
IFS=","
read -p "Enter 2 numbers to add separated by a comma" num1 num2
num1=${num1//[[:blank:]]/}
num2=${num1//[[:blank:]]/}
sum=$((num1+num2))
sum "$num1 + $num2 = $sum"
IFS="$OIFS"
name="name"
echo "${name}'s toy"
samp_string ="Thr dog climed the tree"
echo "${samp_string//dog/cat}"
echo "I am ${name:=name}"
#case
read -p "How old are you: " age
case $age in
[0-4])
echo "To young for school"
;;
5)
echo "Go to Kindergarten"
;;
[6-9]|1[0-8])
grade=$((age-5))
echo "Go to grade $grade"
;;
*)
echo "You are to old for school"
;;
esac
can_vote= 0
age=18
((age >=18?(can_vote=1):(can_vote=0)))
echo "Can Vote : $can_vote"
rand_str="A randomstrging"
echo "String Length : ${#rand_str}"
echo "${rand_str:2}"
echo "${rand_str:2:7}"
2)Indirect expansion
If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion.
However, reading on from there:
The exceptions to this are the expansions of${!prefix*}and${!name[@]}described below.${!prefix*}Names matching prefix. Expands to the names of variables whose names begin with prefix, separated by the first character of theIFSspecial variable.
In other words, your particular example
${!N*} is an exception to the rule you quoted. It does, however, work as advertised in the expected cases, such as:$ export xyzzy=plugh ; export plugh=cave
$ echo ${xyzzy} # normal, xyzzy to plugh
plugh
$ echo ${!xyzzy} # indirection, xyzzy to plugh to cave
cave
3)
It refers to the positional parameters
$1 ... $n.${1:-default} means "if parameter 1 is unset or empty, then use default instead".
Caveat: do not confuse
${1:-2} with ${1: -2}. With bash, the latter is substituted with the last two characters of $1.
Example:
$ set --
$ echo "${1:-2}"
2
$ set 345 678
$ echo "${1:-2}"
345
$ echo "${1: -2}"
45
4)
This technique allows for a variable to be assigned a value if another variable is either empty or is undefined. NOTE: This "other variable" can be the same or another variable.
excerpt
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted.
Otherwise, the value of parameter is substituted.
NOTE: This form also works,
${parameter-word}. If you'd like to see a full list of all forms of parameter expansion available within Bash then I highly suggest you take a look at this topic in the Bash Hacker's wiki titled: "Parameter expansion".Examples
variable doesn't exist$ echo "$VAR1"
$ VAR1="${VAR1:-default value}"
$ echo "$VAR1"
default value
variable exists$ VAR1="has value"
$ echo "$VAR1"
has value
$ VAR1="${VAR1:-default value}"
$ echo "$VAR1"
has value
The same thing can be done by evaluating other variables, or running commands within the default value portion of the notation.
$ VAR2="has another value"
$ echo "$VAR2"
has another value
$ echo "$VAR1"
$
$ VAR1="${VAR1:-$VAR2}"
$ echo "$VAR1"
has another value
More Examples
You can also use a slightly different notation where it's just
VARX=${VARX-<def. value>}.$ echo "${VAR1-0}"
has another value
$ echo "${VAR2-0}"
has another value
$ echo "${VAR3-0}"
0
In the above
$VAR1 & $VAR2 were already defined with the string "has another value" but $VAR3 was undefined, so the default value was used instead, 0.Another Example
$ VARX="${VAR3-0}"
$ echo "$VARX"
0
Checking and assigning using := notation
Lastly I'll mention the handy operator,
:=. This will do a check and assign a value if the variable under test is empty or undefined.Example
Notice that
$VAR1 is now set. The operator := did the test and the assignment in a single operation.$ unset VAR1
$ echo "$VAR1"
$ echo "${VAR1:=default}"
default
$ echo "$VAR1"
default
However if the value is set prior, then it's left alone.
$ VAR1="some value"
$ echo "${VAR1:=default}"
some value
$ echo "$VAR1"
some value
Handy Dandy Reference Table
References
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20) what to use in the script insted of "if" - Best Practices
a) exit codes
; - to chain commands together
echo "sth" ; echo "sth2"
b) logical operators
&& - "and and" only runs second command if first succeed
cat file && echo "sth2"
|| - "or or" it runs second command only if first one not succeed
cat file || echo "sth2"
& - |"and" it runs both of command one after another without wait for first command to end running
sleep 5 & echo "sth2"
21)
100)
a)https://www.youtube.com/watch?v=hwrnmQumtPw

Komentarze
Prześlij komentarz