mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 22:02:24 +03:00
created shell dir
This commit is contained in:
37
shell/AWK.txt
Normal file
37
shell/AWK.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
AWK
|
||||
|
||||
* searches files for lines that contain certain pattern
|
||||
* performs operation described in AWK body on line with certain
|
||||
pattern.
|
||||
* performs operation described in AWK body on choosen line
|
||||
|
||||
|
||||
* Basic structure
|
||||
|
||||
awk 'program_you_will_write' input-file1 input-file2
|
||||
|
||||
awk 'BEGIN{
|
||||
code_in_BEGIN_section
|
||||
}
|
||||
|
||||
{ code_in_main_body_section }
|
||||
|
||||
END{
|
||||
code_in_END_section
|
||||
}' input-file1 input-file2
|
||||
|
||||
* AWK Commands
|
||||
|
||||
* In AWK body bash features doesn't work
|
||||
* Completely new language in AWK body
|
||||
* print
|
||||
* if-else
|
||||
* for loop
|
||||
* ...
|
||||
|
||||
* Completely new features we can call on specific line (specific text)
|
||||
* NF (number of fields)
|
||||
* NR (number of records)
|
||||
* RS (records separator)
|
||||
* $1,$2,.. (specific fields)
|
||||
* ...
|
||||
662
shell/Bash-Scripting.txt
Normal file
662
shell/Bash-Scripting.txt
Normal file
@@ -0,0 +1,662 @@
|
||||
-> ls :
|
||||
|
||||
List all the file or folder present in the current directory.
|
||||
|
||||
Option:
|
||||
|
||||
* -l : List all the file and folder with permission, owner,
|
||||
and group details in form of vertically alinged list.
|
||||
|
||||
* -a : Display hidden (.)dotfile as well.
|
||||
|
||||
-> pwd :
|
||||
|
||||
Print the current directory, present on the terminal.
|
||||
|
||||
-> date :
|
||||
|
||||
Print the current date and time.
|
||||
|
||||
-> chmod :
|
||||
|
||||
Used to change the permission of file or folder
|
||||
|
||||
Symbol Meaning :
|
||||
|
||||
* a : For all (including owner, group and others)
|
||||
* u : For owner
|
||||
* g : For group
|
||||
* o : For others
|
||||
* r : read permission ( number 4 )
|
||||
* w : write permission ( number 2 )
|
||||
* x : execute permission ( number 1 )
|
||||
|
||||
Option :
|
||||
|
||||
* a+x : Add execute permission for all.
|
||||
* a-x : Remove execute permission for all.
|
||||
* 755 : rwx - user , r-x - group, r-x - others
|
||||
|
||||
How to use?
|
||||
|
||||
chmod a+x "file/folder name"
|
||||
chmod 755 "file/folder name"
|
||||
|
||||
-> echo :
|
||||
|
||||
To Print argument on the terminal
|
||||
|
||||
Option:
|
||||
|
||||
* -n : Not to add newline.
|
||||
* ".." : String to print
|
||||
* $PATH : variable ( environment variable, etc. )
|
||||
|
||||
-> $PATH environment variable :
|
||||
|
||||
Store the paths which help in invoking executable, script, etc,
|
||||
without specifying whole path, where its present.
|
||||
|
||||
How to add path to $PATH?
|
||||
|
||||
export PATH=$PATH:$(pwd)
|
||||
or full path to intended directory.
|
||||
|
||||
-> SHABANG (#!/bin/bash)
|
||||
|
||||
Tells to use this interpreter to run the script.
|
||||
Same as /bin/bash ./script
|
||||
|
||||
-> Variables :
|
||||
|
||||
To stores data and can be changed.
|
||||
|
||||
1. Explicit Defination :
|
||||
VAR=value
|
||||
COUNT=5
|
||||
PATH=/var/lib
|
||||
|
||||
2. Read command :
|
||||
read VAR
|
||||
read COUNT
|
||||
read PATH
|
||||
|
||||
Option :
|
||||
|
||||
* -p : to print the parameter before read.
|
||||
|
||||
How to use?
|
||||
|
||||
read -p "Your name: " NAME
|
||||
|
||||
* -s : to hide the input to be taken. Best use
|
||||
for password.
|
||||
|
||||
How to use?
|
||||
|
||||
read -sp "Your age: " AGE
|
||||
|
||||
3. Command Substitution :
|
||||
|
||||
To store the output of the command executed to the variable.
|
||||
|
||||
How to use?
|
||||
|
||||
VAR=$(pwd) or VAR=`pwd`
|
||||
|
||||
|
||||
How to use variables?
|
||||
|
||||
Use $variableName for using variables.
|
||||
|
||||
|
||||
-> For Math Calculation :
|
||||
|
||||
1 . let
|
||||
|
||||
let RESULT=NUMBER+5
|
||||
let NUMBER++
|
||||
let NUMBER--
|
||||
let NUMBER+=5
|
||||
let NUMBER-=5
|
||||
|
||||
2. (( ))
|
||||
|
||||
RESULT=$(( NUMBER + 5 ))
|
||||
|
||||
3. []
|
||||
|
||||
RESULT=$[ NUMBER + 5 ]
|
||||
|
||||
4. expr
|
||||
|
||||
RESULT=$(expr $NUMBER + 5)
|
||||
RESULT=`expr $NUMBER + 5`
|
||||
RESULT=`expr 2 + 3`
|
||||
|
||||
5. bc ( used for floating point number )
|
||||
|
||||
RESULT=`echo "$NUMBER * 1.9" | bc`
|
||||
|
||||
|
||||
-> Argument :
|
||||
|
||||
|
||||
$0 - script name
|
||||
$1 - first argument
|
||||
$2 - second argument
|
||||
$n - nth argument
|
||||
"$@" - all argument, expands as "$1" "$2" "$3"
|
||||
and so on.
|
||||
"$*" - all argument as string, "$1c$2c$3",
|
||||
where c is IFS (Internal field
|
||||
separator), which is usally whitespace.
|
||||
$# - arguments count
|
||||
|
||||
-> Redirecting and piping:
|
||||
|
||||
Redirecting
|
||||
|
||||
* STDIN (0) - Standard input (data provided to program)
|
||||
* STDOUT (1) - Standard output (what program prints, defaultly to the terminal)
|
||||
* STDERR (2) - Standard error (what error message program prints, defaultly to terminal)
|
||||
|
||||
examples:
|
||||
cat file1.txt > output_from_cat.txt
|
||||
cat file1.txt 1>output_from_cat.txt
|
||||
cat file1.txt 2>error.txt
|
||||
cat file1.txt 1>output_from_cat.txt 2>error.txt
|
||||
cat file1.txt &> output_from_cat.txt (from both 1 and 2)
|
||||
cat file1.txt 1> output_from_cat.txt 2>&1(redirecting error to output)
|
||||
|
||||
cat file1.txt >> output_from_cat.txt(adding output instead of replacing it)
|
||||
|
||||
Piping
|
||||
|
||||
output of preceeding program is input to succeeding
|
||||
program.
|
||||
|
||||
Program1 | Program2
|
||||
|
||||
examples:
|
||||
cat file.txt | wc -l
|
||||
cat file.txt | head -5 | tail -3 | wc -l
|
||||
|
||||
-> Exit Status
|
||||
|
||||
echo $?
|
||||
|
||||
$? will give a return value of previous command.
|
||||
It's the return value, called EXIT STATUS.
|
||||
Successfully exiting give 0 value otherwise
|
||||
non-zero value.
|
||||
|
||||
We can exit script using exit command.
|
||||
exit command can be used with number 0 - 255.
|
||||
|
||||
-> If Statement
|
||||
|
||||
if[ condition ]; then
|
||||
....
|
||||
elif[ condition ]; then
|
||||
....
|
||||
elif[ condition ]; then
|
||||
....
|
||||
else
|
||||
....
|
||||
fi
|
||||
|
||||
* AND with if statement ?
|
||||
|
||||
if[ condition ] && [ condition ]; then
|
||||
....
|
||||
fi
|
||||
|
||||
* OR wiht if statement ?
|
||||
|
||||
if[ condition ] || [ condition ]; then
|
||||
....
|
||||
fi
|
||||
|
||||
* Negate if condition
|
||||
|
||||
if[ !condition ]; then
|
||||
....
|
||||
fi
|
||||
|
||||
if![ condition ]; then
|
||||
....
|
||||
fi
|
||||
|
||||
-> Mathematical comparison:
|
||||
|
||||
* Operands
|
||||
|
||||
$ -eq equal to
|
||||
$ -ne not equal to
|
||||
$ -gt greater than
|
||||
$ -lt less than
|
||||
$ -ge greater than or equal to
|
||||
$ -le less than or equal to
|
||||
|
||||
Examples:
|
||||
|
||||
if [ $VAR -eq 5 ]; then
|
||||
if [ $VAR -gt 7 ]; then
|
||||
if [ $VAR -eq 5 ] && [ $COUNT -ne 1 ]; then
|
||||
|
||||
|
||||
|
||||
-> String Comaparison
|
||||
|
||||
* Comapare two strings
|
||||
[ "$STR1" = "$STR2" ]
|
||||
[ "$STR1" != "$STR2" ]
|
||||
|
||||
[ "$STR1" = "hello" ]
|
||||
[ "$STR1" != "hello" ]
|
||||
|
||||
* Compare two strings - quotation marks?
|
||||
Bash can handle it in another way [[ ]]
|
||||
|
||||
[[ $STR1 = $STR2 ]]
|
||||
[[ $STR1 != $STR2 ]]
|
||||
|
||||
[[ $STR1 = "hello" ]]
|
||||
[[ $STR1 != "hello ]]
|
||||
|
||||
This is not widely used and most scripts
|
||||
are using quotation marks with single
|
||||
brackets
|
||||
|
||||
* Test if string is empty
|
||||
|
||||
[ -z "$STR1" ] returns true if STR1 holds
|
||||
an empty string
|
||||
|
||||
[[ -z $STR1 ]] possible, but not widely used
|
||||
|
||||
[ -n "$STR1" ] returns true if STR1 holds
|
||||
a non-empty string
|
||||
|
||||
[[ -n $STR1 ]] possible, but not widely used
|
||||
|
||||
* Alphabetically compare two strings
|
||||
|
||||
[[ $STR1 > $STR2 ]]
|
||||
|
||||
[[ $STR1 < $STR2 ]]
|
||||
|
||||
No choice!!!
|
||||
|
||||
* Wildcards
|
||||
|
||||
[[ $STR1 == string-with-wildcards ]]
|
||||
|
||||
* Regular expression
|
||||
|
||||
[[ $STR1 =~ $regex ]]
|
||||
|
||||
|
||||
-> Wildcards (globbing patterns)
|
||||
|-----------------------------------------------------------------------|
|
||||
| Symbol | Description | Example | Example matches |
|
||||
|-----------------------------------------------------------------------|
|
||||
| ? | Single | hel? | help, hell, |
|
||||
| | character | | hel1.... |
|
||||
|-----------------------------------------------------------------------|
|
||||
| * | Any number of | ca* | car,ca,carpet, |
|
||||
| | characters | | carpenter,car112.. |
|
||||
| | (including zero)| | |
|
||||
|-----------------------------------------------------------------------|
|
||||
| [] | Single character| file[0-2] | file0,file1,file2 |
|
||||
| | from range | [hd]ello | hello or dello |
|
||||
|-----------------------------------------------------------------------|
|
||||
| {} | Comma seperated | {*.txt, | hello.txt,doc.txt, |
|
||||
| | terms | *.pdf} | source.pdf, book.pdf|
|
||||
|-----------------------------------------------------------------------|
|
||||
| [!] | Any character | file[!1] | file2,file3.... |
|
||||
| | listed in | | |
|
||||
| | brackets | | |
|
||||
|-----------------------------------------------------------------------|
|
||||
|
||||
* some useful character classes: (use it within [])
|
||||
|
||||
|-------------------------------------------|
|
||||
| [:upper:] | Uppercase character |
|
||||
| [:lower:] | Lowercase character |
|
||||
| [:alpha:] | Alphabetic character |
|
||||
| [:digit:] | Number character |
|
||||
| [:alnum:] | Alphanumric character |
|
||||
| | (alpha + digit) |
|
||||
| [:space:] | whitespace character |
|
||||
| | (space, tab, newline) |
|
||||
|-------------------------------------------|
|
||||
|
||||
* [[ $STRING == pattern_with_wildcards ]]
|
||||
* [[ $STRING == file[0-9].txt ]]
|
||||
* [[ $STRING == rich* ]]
|
||||
|
||||
|
||||
-> Regular Expressions
|
||||
|
||||
|-----------------------------------------------------------|
|
||||
| Symbol | Description | Example | Example matches |
|
||||
|-----------------------------------------------------------|
|
||||
| . | any single | bo.k | book,bo1k, ... |
|
||||
| | character | | |
|
||||
|-----------------------------------------------------------|
|
||||
| * | preceeding | co*l | cl,col,cool, ...|
|
||||
| | item must | | |
|
||||
| | match zero | | |
|
||||
| | or more | | |
|
||||
| | times | | |
|
||||
|-----------------------------------------------------------|
|
||||
| ^ | Start of | ^hello | line starting |
|
||||
| | the line | | with hello |
|
||||
| | marker | | |
|
||||
|-----------------------------------------------------------|
|
||||
| $ | End of the | hello$ | line ending |
|
||||
| | line marker | | with hello |
|
||||
|-----------------------------------------------------------|
|
||||
| [] | Any one of | coo[kl] | cook or cool |
|
||||
| | of character| | |
|
||||
| | enclosed in[]| | |
|
||||
|-----------------------------------------------------------|
|
||||
| [-] | Any character| file[1-3]| file1,file2,file3|
|
||||
| | within the | | |
|
||||
| | range | | |
|
||||
|-----------------------------------------------------------|
|
||||
| [^] | Any character| file[^0 | file8,file9 |
|
||||
| | except those| 1234567]| |
|
||||
| | enclosed in | | |
|
||||
| | brackets | | |
|
||||
|-----------------------------------------------------------|
|
||||
| ? | preceeding | colou?r | color,colour |
|
||||
| | item must | | NOT colouur |
|
||||
| | one or zero | | |
|
||||
| | times | | |
|
||||
|-----------------------------------------------------------|
|
||||
| + | preceeding | file1+ | file1,file11, |
|
||||
| | item must | | file111 |
|
||||
| | match one or | | NOT file |
|
||||
| | more times | | |
|
||||
|-----------------------------------------------------------|
|
||||
| {n} | preceeding | [0-9]{3}| Any three digit:|
|
||||
| | item must | | 097,256,787,... |
|
||||
| | match n times | | |
|
||||
|-----------------------------------------------------------|
|
||||
| {n,} | Minimum number| [0-9]{3,}| Any three digit |
|
||||
| | of times the | | 111,097,256,787.|
|
||||
| | preceeding | | |
|
||||
| | item should | | |
|
||||
| | match | | |
|
||||
|-----------------------------------------------------------|
|
||||
| {n,m} | Minimum and | [0-9]{1,3}| 1,158,26, ... |
|
||||
| | maximum number| | NOT 1258,1111,..|
|
||||
| | of times the | | |
|
||||
| | preceeding | | |
|
||||
| | item should | | |
|
||||
| | match | | |
|
||||
|-----------------------------------------------------------|
|
||||
| \ | Escape | hell\.o | hell.o |
|
||||
| | character - | | NOT:helllo, |
|
||||
| | escape any of | | hell1o... |
|
||||
| | the special | | |
|
||||
| | character, | | |
|
||||
| | ignore the | | |
|
||||
| | meaning of | | |
|
||||
| | them | | |
|
||||
|-----------------------------------------------------------|
|
||||
|
||||
-> Regex in Bash
|
||||
|
||||
* [[ $STRING =~ $REGEX ]]
|
||||
* REGEX="http://.*\.jpg"
|
||||
* ${BASH_REMATCH[0]} -> part of the STRING which match
|
||||
REGEX (e.g. http://images.jpg)
|
||||
* ${BASH_REMATCH[1]} -> part of the REGEX which is
|
||||
enclosed in first parentheses,
|
||||
e.g.
|
||||
REGEX="http://(.*)\.jpg" -> which
|
||||
would represents as "images"
|
||||
|
||||
|
||||
-> Filesystem Related Tests
|
||||
|
||||
|-----------------------------------------------------------------------|
|
||||
| File Test operators | Description, what is returned |
|
||||
|-----------------------------------------------------------------------|
|
||||
| [ -e $VAR ] | True if variable hold existing file (file |
|
||||
| | or directory) |
|
||||
|-----------------------------------------------------------------------|
|
||||
| [ -f $VAR ] | True if variable holds an existing regular |
|
||||
| | file |
|
||||
|-----------------------------------------------------------------------|
|
||||
| [ -d $VAR ] | True if variable holds an existing directory|
|
||||
|-----------------------------------------------------------------------|
|
||||
| [ -x $VAR ] | True if variable is an executable file |
|
||||
|-----------------------------------------------------------------------|
|
||||
| [ -L $VAR ] | True if variable holds th path of a symlink |
|
||||
|-----------------------------------------------------------------------|
|
||||
| [ -r $VAR ] | True if variable holds file that is readable|
|
||||
|-----------------------------------------------------------------------|
|
||||
| [ -w $VAR ] | True if variable holds file that is writable|
|
||||
|-----------------------------------------------------------------------|
|
||||
|
||||
* Example:
|
||||
if [ -f $FILE ]; then
|
||||
echo File exists
|
||||
else
|
||||
echo File does not exists
|
||||
fi
|
||||
|
||||
-> && and ||
|
||||
|
||||
*examples
|
||||
[ $? -eq 0 ] && echo command OK || echo something went wrong
|
||||
|-----------------| |
|
||||
if true execute this |
|
||||
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
|
||||
9
shell/arguments.sh
Normal file
9
shell/arguments.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
IFS=","
|
||||
echo "Script name: $0"
|
||||
echo "First argument: $1"
|
||||
echo "Second argument: $2"
|
||||
echo "All arguments with \$@: $@"
|
||||
echo "All arguments with \$*: $*"
|
||||
echo "Arguments count: $#"
|
||||
17
shell/array.sh
Normal file
17
shell/array.sh
Normal 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
shell/case.sh
Normal file
43
shell/case.sh
Normal 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
|
||||
49
shell/filetest.sh
Normal file
49
shell/filetest.sh
Normal file
@@ -0,0 +1,49 @@
|
||||
#!/bin/bash
|
||||
|
||||
#for provided file print the summary of what permissions users has granted
|
||||
#example: ./filetest.sh hello.txt
|
||||
#READ: YES
|
||||
#WRITE: NO
|
||||
#EXECUTABLE:NO
|
||||
|
||||
#argument check
|
||||
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "Provide exactly one argument"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
#variable assignment
|
||||
FILE=$1
|
||||
|
||||
#check if the file exists
|
||||
if [ -f $FILE ]; then
|
||||
|
||||
#default variables
|
||||
VAR_READ="NO"
|
||||
VAR_WRITE="NO"
|
||||
VAR_EXEC="NO"
|
||||
|
||||
#check if file is readable
|
||||
if [ -r $FILE ]; then
|
||||
VAR_READ="YES"
|
||||
fi
|
||||
|
||||
#check if file is writable
|
||||
if [ -w $FILE ]; then
|
||||
VAR_WRITE="YES"
|
||||
fi
|
||||
|
||||
#check if file is executable
|
||||
if [ -x $FILE ]; then
|
||||
VAR_EXEC="YES"
|
||||
fi
|
||||
|
||||
#write permissions summary to user
|
||||
echo "==FILE: $FILE=="
|
||||
echo "READ: $VAR_READ"
|
||||
echo "WRITE: $VAR_WRITE"
|
||||
echo "EXECUTABLE: $VAR_EXEC"
|
||||
else
|
||||
echo $FILE does not exists
|
||||
fi
|
||||
25
shell/function.sh
Normal file
25
shell/function.sh
Normal 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
shell/getopt.sh
Normal file
40
shell/getopt.sh
Normal 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
shell/getopts.sh
Normal file
15
shell/getopts.sh
Normal 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
shell/print.sh
Normal file
25
shell/print.sh
Normal 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"
|
||||
38
shell/regex.sh
Normal file
38
shell/regex.sh
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
|
||||
#provide one word/sentence as an argument to the script.
|
||||
#If in that sentence will be ip addresss, find out, if that
|
||||
#ip address is reachable or not.
|
||||
|
||||
#argument check
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "Provide exactly one argument e.g. $0 argument"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VAR1=$1
|
||||
#ip address regex
|
||||
REGEX="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[[:digit:]]{1,3}"
|
||||
|
||||
#regex check
|
||||
if ! [[ $VAR1 =~ $REGEX ]]; then
|
||||
echo "No IP address provided"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
IP=${BASH_REMATCH[0]}
|
||||
|
||||
#find if ip address is reachable or not
|
||||
ping -c4 $IP 1>/dev/null
|
||||
|
||||
#1>/dev/null
|
||||
#It will redirect standard output and prevent
|
||||
#any output to print on screen.
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
STATUS="ALIVE"
|
||||
else
|
||||
STATUS="DEAD"
|
||||
fi
|
||||
|
||||
echo "IP found: $IP ($STATUS)"
|
||||
12
shell/secret.sh
Normal file
12
shell/secret.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo -n Your age:
|
||||
read AGE
|
||||
|
||||
#Problem with output as () cause to execute command in subshell.
|
||||
#[ $AGE -lt 20 ] && (echo You are not allowed to see secret; exit 1) || echo Welcome
|
||||
|
||||
#command execute it same shell
|
||||
[ $AGE -lt 20 ] && { echo You are not allowed to see secret; exit 1;} || echo Welcome
|
||||
|
||||
echo Secret is that there is no secret
|
||||
4
shell/text.txt
Normal file
4
shell/text.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
line 1
|
||||
line 2
|
||||
something 3
|
||||
something 4
|
||||
13
shell/time_measurement.sh
Normal file
13
shell/time_measurement.sh
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
#start time measurement from here
|
||||
START=$(date +%s)
|
||||
CURRENT_DIRECTORY=$(pwd)
|
||||
sleep 2 #sleep 2 seconds
|
||||
END=$(date +%s)
|
||||
|
||||
#end time measurement
|
||||
DIFFERENCE=$(( END - START ))
|
||||
|
||||
echo
|
||||
echo Time elapsed: $DIFFERENCE seconds.
|
||||
Reference in New Issue
Block a user