Basic scripting is done

This commit is contained in:
Hizenberg469
2024-11-30 11:20:46 +05:30
parent 1ede16f3c4
commit 04859194ac
8 changed files with 369 additions and 1 deletions

View File

@@ -461,3 +461,202 @@
|--------------------| |--------------------|
-> 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

17
array.sh Normal file
View File

@@ -0,0 +1,17 @@
#!/bin/bash
ARRAY=($(ls *.txt))
COUNT=0
echo -e "FILE NAME \t WRITEABLE"
for FILE in "${ARRAY[@]}"
do
echo -n $FILE
echo -n "[${#ARRAY[$COUNT]}]"
if [ -w "$FILE" ]; then
echo -e "\t YES"
else
echo -e "\t NO"
fi
let COUNT++
done

43
case.sh Normal file
View File

@@ -0,0 +1,43 @@
#!/bin/bash
#read data from users and make them option to recheck
#if provided data are correct
echo -n "Enter your name: "
read NAME
echo -n "Enter your age: "
read AGE
echo -n "Enter your department: "
read DEP
echo "You've entered following: "
echo "Name: $NAME"
echo "AGE: $AGE"
echo "Department: $DEP"
CHECK=0
while [ $CHECK -eq 0 ]
do
echo -n "Is that correct? [Y/n] "
read ANSWER
case "$ANSWER" in
"Y"|"y" )
echo "Name: $NAME" >> empl.txt
echo "AGE: $AGE" >> empl.txt
echo "Department: $DEP" >> empl.txt
echo "================================" >> empl.txt
echo "Your data were saved into employee file"
CHECK=1
;;
"N"|"n" )
echo "Nothing was saved, run the script again \
if you want to add data to employee file"
CHECK=1
;;
* )
echo "Wrong answer provided"
esac
done

25
function.sh Normal file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
function addition {
local FIRST=$1
local SECOND=$2
let RESULT=FIRST+SECOND
echo "Result is: $RESULT"
let FIRST++
let SECOND++
return $RESULT
}
#do the addition of two numbers
echo -n "Enter first number: "
read FIRST
echo -n "Enter second number: "
read SECOND
addition $FIRST $SECOND
echo "RETURN: $?"
echo "Printing variable:"
echo "FIRST: $FIRST"
echo "SECOND: $SECOND"

40
getopt.sh Normal file
View File

@@ -0,0 +1,40 @@
#!/bin/bash
echo "All arguments: $@"
opts=`getopt -o a::b:cde --long file::,name:,help -- "$@"`
eval set -- "$opts"
echo "All arguments after getopt: $@"
while [ $# -gt 0 ]
do
case "$1" in
-a) echo "parameter 'a' selected with argument: $2"
shift 2 #same of 2 shift command
;;
-b) echo "parameter 'b' selected with argument: $2"
shift 2
;;
-c) echo "paramter 'c' selected"
shift
;;
-d) echo "paramter 'd' selected"
shift
;;
-e) echo "paramter 'e' selected"
shift
;;
--file) echo "parameter 'file' selected with argument: $2"
shift 2
;;
--name) echo "parameter 'name' selected with argument: $2"
shift 2
;;
--help) echo "parameter 'help' selected"
shift
;;
*) echo "something else: $1"
shift
;;
esac
done

15
getopts.sh Normal file
View File

@@ -0,0 +1,15 @@
#!/bin/bash
while getopts a:b:cd param;
do
case $param in
a) echo "parameter 'a' with argument: $OPTARG"
;;
b) echo "parameter 'b' selected with argument: $OPTARG"
;;
c) echo "parameter 'c' selected (no colon, no argument)"
;;
d) echo "parameter 'd' selected"
;;
esac
done

25
print.sh Normal file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
#create script for printing files, which will display
#also line numbers
#argument check
if [ $# -ne 1 ]; then
echo "Exactly one arugment is needed, run: $0 file-path"
exit 1
fi
#check if provided argument is a file
if ! [ -f "$1" ]; then
echo "File you've specified doesn't exist"
exit 2
fi
FILENAME=$1
COUNT=1
while read line
do
echo "$COUNT: $line"
let COUNT++
done < "$FILENAME"

4
text.txt Normal file
View File

@@ -0,0 +1,4 @@
line 1
line 2
something 3
something 4