mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 22:02:24 +03:00
300 lines
6.1 KiB
Plaintext
300 lines
6.1 KiB
Plaintext
-> 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 ]]
|
|
|
|
|