mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 22:02:24 +03:00
Basic scripting is done
This commit is contained in:
@@ -460,4 +460,203 @@
|
||||
if NOT true |
|
||||
|--------------------|
|
||||
|
||||
|
||||
|
||||
-> For loops
|
||||
|
||||
for arg in [list]
|
||||
do
|
||||
command(s)..
|
||||
done
|
||||
|
||||
* Example
|
||||
for PLANET in "Mercury Venus Earth"
|
||||
do
|
||||
echo $PLANET
|
||||
done
|
||||
|
||||
for file in *.txt
|
||||
do
|
||||
$file
|
||||
done
|
||||
|
||||
-> For loops - C style for loops
|
||||
|
||||
for ((i=1; i <= 10 ; i++))
|
||||
do
|
||||
echo $i
|
||||
done
|
||||
|
||||
-> While loop
|
||||
|
||||
while [ condition ]
|
||||
do
|
||||
command(s)
|
||||
done
|
||||
|
||||
-> C-style While loop
|
||||
|
||||
while (( condition ))
|
||||
do
|
||||
command(s)
|
||||
done
|
||||
|
||||
* Example
|
||||
A=1
|
||||
LIMIT=10
|
||||
while (( A < LIMIT )) //no $ here
|
||||
do
|
||||
touch $A
|
||||
let A++
|
||||
done
|
||||
|
||||
-> Reading Files with While loop
|
||||
|
||||
while read line
|
||||
do
|
||||
command(s)..
|
||||
done < "$FILENAME"
|
||||
|
||||
cat "$FILENAME" |
|
||||
while read line
|
||||
do
|
||||
echo $line
|
||||
done
|
||||
|
||||
-> Case:
|
||||
|
||||
case "$VAR" in
|
||||
|
||||
"$condition1")
|
||||
command(s)...
|
||||
;;
|
||||
"$condition2")
|
||||
command(s)...
|
||||
;;
|
||||
*)
|
||||
command(s)..
|
||||
;;
|
||||
esac
|
||||
|
||||
*Example:
|
||||
|
||||
case "$(whoami)" in
|
||||
|
||||
"root")
|
||||
echo you are root
|
||||
;;
|
||||
"richard"|"susan")
|
||||
echo you are user
|
||||
;;
|
||||
*)
|
||||
echo I don't know you
|
||||
;;
|
||||
esac
|
||||
|
||||
-> shift
|
||||
It's a command to throw away the parameter count
|
||||
that is given to the script.
|
||||
|
||||
|
||||
-> getopts:
|
||||
|
||||
* easy way how to parse short positional parameters (-f)
|
||||
* doesn't support long positional parameters ( --help )
|
||||
|
||||
* getopts optstring name
|
||||
|
||||
-> For examples:
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
while getopts a:b:cd param; do
|
||||
case $param in OPTIND is initialized to 1
|
||||
a) echo "" each time the shell or a
|
||||
echo "parameter 'a' with argument: $OPTARG" shell script is invoked.
|
||||
;;
|
||||
b) echo "parameter 'b' with argument: $OPTARG" getopts places index of the
|
||||
;; next argument to be
|
||||
c) echo "parameter 'c' selected (no colon, no argument)" processed into the variable
|
||||
;; OPTIND
|
||||
d) echo "parameter 'd' selected (no colon, no argument)"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
||||
-> getopt:
|
||||
|
||||
* parse command options (enhanced)
|
||||
|
||||
opts=`getopt -o a::b:cde --long file::,name::,help -- "$@"`
|
||||
eval set -- "$opts"
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
echo "All arguments: $@"
|
||||
opts=`getopt -o a::b:cde --long file::,name:,help -- "$@"`
|
||||
eval set -- "$opts"
|
||||
echo "All arguments after getopt: $@"
|
||||
|
||||
-> Arrays:
|
||||
|
||||
* Declaration
|
||||
* ARRAY=(value1 value2 .. valueN)
|
||||
* ARRAY=(one,two,three)
|
||||
|
||||
* Calling
|
||||
* ${ARRAY[0]} #one
|
||||
* ${ARRAY[1]} #two
|
||||
* ${ARRAY[2]} #three
|
||||
|
||||
|
||||
* ARRAY=(one,two,three)
|
||||
|
||||
${ARRAY[0]} #one
|
||||
${ARRAY[1]} #two
|
||||
${ARRAY[2]} #three
|
||||
${ARRAY[@]} #all items in Arrays
|
||||
${ARRAY[*]} #all items in array, delimited by first character of IFS
|
||||
${!ARRAY[@]} #all indexes in the array ( @/* )
|
||||
${#ARRAY[@]} #number of items in the array ( @/* )
|
||||
${#ARRAY[0]} #length of item zero
|
||||
|
||||
-> Functions
|
||||
|
||||
* function_name(){
|
||||
<commands>
|
||||
}
|
||||
|
||||
* function function_name{
|
||||
<commands>
|
||||
}
|
||||
|
||||
* Example
|
||||
| function hello{
|
||||
| echo hello
|
||||
| }
|
||||
|
|
||||
| echo "output from function hello: "
|
||||
| hello
|
||||
|
||||
|
||||
|
||||
| function hello{
|
||||
| echo hello $1
|
||||
| return 11
|
||||
| }
|
||||
|
|
||||
| echo "output from function hello: "
|
||||
| hello richard
|
||||
| echo "return value from function hello is: $?"
|
||||
|
||||
|
||||
|
||||
| function hello { function hello{
|
||||
| name=$1 local name=$1
|
||||
| echo "hello $name" echo "hello $name"
|
||||
| } }
|
||||
|
|
||||
| echo "enter your name:" echo "enter your name:"
|
||||
| read name #richard read name #richard
|
||||
| hello Peter #hello Peter hello Peter #hello Peter
|
||||
| echo $name #Peter echo $name #richard
|
||||
|
||||
Reference in New Issue
Block a user