From 04859194acec133d3636194009e3f939e0a24e01 Mon Sep 17 00:00:00 2001 From: Hizenberg469 Date: Sat, 30 Nov 2024 11:20:46 +0530 Subject: [PATCH] Basic scripting is done --- Bash-Scripting.txt | 201 ++++++++++++++++++++++++++++++++++++++++++++- array.sh | 17 ++++ case.sh | 43 ++++++++++ function.sh | 25 ++++++ getopt.sh | 40 +++++++++ getopts.sh | 15 ++++ print.sh | 25 ++++++ text.txt | 4 + 8 files changed, 369 insertions(+), 1 deletion(-) create mode 100644 array.sh create mode 100644 case.sh create mode 100644 function.sh create mode 100644 getopt.sh create mode 100644 getopts.sh create mode 100644 print.sh create mode 100644 text.txt diff --git a/Bash-Scripting.txt b/Bash-Scripting.txt index 3945ceb..c76a955 100644 --- a/Bash-Scripting.txt +++ b/Bash-Scripting.txt @@ -460,4 +460,203 @@ if NOT true | |--------------------| - \ No newline at end of file + +-> 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(){ + + } + + * function function_name{ + + } + + * 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 diff --git a/array.sh b/array.sh new file mode 100644 index 0000000..c53bcb5 --- /dev/null +++ b/array.sh @@ -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 \ No newline at end of file diff --git a/case.sh b/case.sh new file mode 100644 index 0000000..2a3d913 --- /dev/null +++ b/case.sh @@ -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 \ No newline at end of file diff --git a/function.sh b/function.sh new file mode 100644 index 0000000..db0d15a --- /dev/null +++ b/function.sh @@ -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" \ No newline at end of file diff --git a/getopt.sh b/getopt.sh new file mode 100644 index 0000000..18ce8f2 --- /dev/null +++ b/getopt.sh @@ -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 \ No newline at end of file diff --git a/getopts.sh b/getopts.sh new file mode 100644 index 0000000..e9b967c --- /dev/null +++ b/getopts.sh @@ -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 \ No newline at end of file diff --git a/print.sh b/print.sh new file mode 100644 index 0000000..5287a00 --- /dev/null +++ b/print.sh @@ -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" \ No newline at end of file diff --git a/text.txt b/text.txt new file mode 100644 index 0000000..5151514 --- /dev/null +++ b/text.txt @@ -0,0 +1,4 @@ +line 1 +line 2 +something 3 +something 4 \ No newline at end of file