Some useful links
Scripting languages are those which do not require an explicit compilation step.
ex: Javascript,Lua.
Programming Languages on the other hand require an explicit compilation step.
ex: C,C++.
Infact whether a language is programming or scripting, depends on the environment,meaning
to say, we can actually write a 'C interpreter' or a 'Javascript compiler'.
https://stackoverflow.com/questions/17253545/scripting-language-vs-programming-language
You probably have seen the starting line of many shell scripts, as the following
#!/bin/bash
This is called shebang
It actually defines which program actually runs the current script.In fact we can use shebang on any file
be it Python, Javascript..
https://unix.stackexchange.com/questions/87560/does-the-shebang-determine-the-shell-which-runs-the-script
The which
command in terminal helps locate where in the linux the command is actually stored.
which bash
/bin/bash
which python
/usr/bin/python
$PATH is an environment variable, which lists colon seperated list of directories.
echo $PATH
/home/mohit123/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:
/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/mohit123/bin
Whenever you type a command on the shell, the command is searched in these directoris, in the order
of their occurrance.
If the command is not found here, an error is displayed.
https://askubuntu.com/questions/551990/what-does-path-mean
Adding a command to one of these directories will make your command accessible from anywhere.
https://unix.stackexchange.com/questions/3809/how-can-i-make-a-program-executable-from-everywhere
How to use floating point in bash.(Bash does not support floating point arithmetic inherently) https://stackoverflow.com/questions/12722095/how-do-i-use-floating-point-division-in-bash
var1=5 #store a constant
var2=$var1 #store a variable
var3=$((var1+var2)) #store the result of an arithmetic expression
var4=$(($var1+$var2)) #store the result of an arithmetic expression
var5=$( ls | wc -l ) #substituting a command/or return value from a function
var6=$( func_name $param1 $param2 ) #storing the return value from a function
It is important that there is no space between the varialble the '=' sign and the rhs.
var1=5
echo 'the value of var1 is $var1'
echo "the value of var1 is $var1"
The first one will print
the value of var1 is $var1
The second one will print
the value of var1 is 5
- Simple Read
- Read with prompt (p)
- Read with silence (s)
- echo
#/bin/bash
echo $1
echo $2
if [ $1 -gt $2 ]
then
echo $1 greater than $2
elif [ $2 -gt $1 ]
then
echo $2 is greater than $1
else
echo equal
fi
Notice that $1 and $2 are the command line args. How to run??
bash example.sh 4 5
4
5
5 is greater than 4
Don't forget the fi
in the end.
-lt : less than
-ge : greater than equal to
-le : less than equal to
-ne : not equal to
-eq : interprets both sides as integer and compares them
== : interprets both sides as string and compares them
https://unix.stackexchange.com/questions/16109/bash-double-equals-vs-eq The following program will output yes since -eq does a integer comparison and
if[[ 01 -eq 1 ]]
then
echo yes
else
echo no
The following program will output no since == does a string comparison and "01" is not equal to "1"
if[[ 01 == 1 ]]
then
echo yes
else
echo no
How to detect whether a directory exists or not.
https://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script?answertab=votes#tab-top
filevar=$1
[ -e filevar ]
filevar="a b c d"
for name in $filevar
do
echo name
done
Output:
a
b
c
d
This will first split the filevar into a
, b
,c
, and d
and hence can be a problem at times,
when we want to treat a b c d
as a single filename(which is not recommended ) we can modify the code
by adding a double quotes around $filevar
in the for loop.
filevar="a b c d"
for name in "$filevar"
do
echo name
done
Output:
a b c d
&&
and ||
follows the same short-ciruit trick as in other languages.
i.e.
[ <cond1> ] && [ <cond2> ]
If [ <cond1> ]
is not satisfied, [ <cond2> ]
is not executed.
Similarly
[ <cond1> ] || [ <cond2> ]
If [ <cond1> ]
is satisfied, [ <cond2> ]
is not executed.
Hence for creating a file we can do:
[ -f "$filevar" ] || [ touch "$filevar" ]
if [ -f "$filevar" ]
is satisfied, it means the file is present, so no need to execute the
second block
Linux Users and groups
Only two individual can change the permission of a file or directory
- The owner(generally the creator of the file)
- The root user https://www.linode.com/docs/tools-reference/linux-users-and-groups