mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 22:02:24 +03:00
Bash scripting
This commit is contained in:
195
Bash-Scripting.txt
Normal file
195
Bash-Scripting.txt
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
-> 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.
|
||||||
9
arguments.sh
Normal file
9
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: $#"
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
AC_INIT(["Name of Program"], [ Version X.X.X], ["author email"])
|
AC_INIT(["Name of Program"], [ Version X.X.X], ["author email"])
|
||||||
|
|
||||||
AM_INIT_AUTOMAKE
|
AM_INIT_AUTOMAKE()
|
||||||
|
|
||||||
AC_CHECK_PROGS(CC,[clang, gcc, xlc, icc], [""])
|
AC_CHECK_PROGS(CC,[clang, gcc, xlc, icc], [""])
|
||||||
AC_PROG_CC
|
AC_PROG_CC
|
||||||
|
|||||||
13
time_measurement.sh
Normal file
13
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