shell-scripting deep dive

Before Learning shell-script we need to know few prerequisite lets know them first things first

https://rafishaikblog.wordpress.com/2016/04/28/shell-scripts/

Comparison of shells
Initially, the UNIX OS used a shell program called Bourne Shell. Then eventually,
many more shell programs were developed for different flavors of UNIX. The
following is brief information about different shells:

•Sh—Bourne Shell
•Csh—C Shell
•Ksh—Korn Shell
•Tcsh—enhanced C Shell
•Bash—GNU Bourne Again Shell
•Zsh—extension to Bash, Ksh, and Tcsh
•Pdksh—extension to KSH

Tasks done by shell
Whenever we type any text in the shell terminal, it is the responsibility of shell to
execute the command properly. The activities done by shell are as follows:
•Reading text and parsing the entered command
•Evaluating metacharacters such as wildcards, special characters,
or history characters
•Process io-redirection, pipes, and background processing
•Signal handling
•Initializing programs for execution

Compiler and interpreter – difference
in process
In any program development, the following are the two options:
Compilation: Using a compiler-based language such as C, C++, Java, and other similar languages

Interpreter: Using interpreter-based languages such as Bash Shell scripting.When we use a compiler-based language, we compile the complete source code, and as a result of compilation, we get a binary executable file. We then execute the binary to check the performance of our program. On the contrary, when we develop the Shell script, such as an interpreter-based program, every line of the program is input to Bash shell. The lines of Shell script are executed one by one sequentially. Even if the second line of a script has an error, the
first line will be executed by the shell interpreter.

vim hello.sh
#!/bin/bash
# This is the first Bash shell
# Scriptname : Hello1.sh
# Written by:Mshaik
echo “Hello $LOGNAME, Have a nice day !”
echo “Your are working in directory `pwd`.”
echo “You are working on a machine called `uname -n`.”
echo “List of files in your directory is.”
ls
echo
# List files in the present working directory
“Bye for now $LOGNAME. The time is `date +%T`!”

The output of executing hello.sh will be as follows:

Hello student, Have a nice day !.
Your are working in directory /home/student/work.
You are working on a machine called ubuntu.
List of files in your directory is.
hello1.sh
hello.sh
Bye for now student. The time is 22:59:03!

###########################3
When not to use scripts
Shell scripts have certain advantages over compiler-based programs, such as C or
C++ language. However, Shell scripting has certain limitations as well.
The following are the advantages:
•Scripts are easy to write
•Scripts are quick to start and easy for debugging
•They save the time of development.
•Tasks of administration are automated
•No additional setup or tools are required for developing or testing
Shell script
The following are the limitations of Shell scripts:
•Every line in Shell script creates a new process in the operating system.
When we execute the compiled program such as C program, it runs as a
single process for the complete program.
•Since every command creates a new process, Shell scripts are slow as
compared to compiled programs.
•Shell scripts are not suitable if heavy math operations are involved.
•There are Problems with cross-platform portability.
•We cannot use Shell scripts in the following situations when:
° ° Extensive file operations are required
° ° We need data structures, such as linked lists or trees
° ° We need to generate or manipulate graphics or GUIs
° ° We need direct access to system hardware
° ° We need a port or socket I/O
° ° We need to use libraries or interface with legacy code
° ° Proprietary, closed source applications are used (Shell scripts put
the source code right out in the open for the entire world to see)

###############################
Working more effectively with
shell – basic commands
Let us learn a few commands, which are required very often, such as man , echo , cat
and similar:
•Enter the following command. It will show the various types of manual
pages displayed by the man command:
$ man man
From the following table, you can get an idea about various types of man
pages for the same command:
Section number Subject area
1 User commands
2 System calls
3 Library calls
4 Special files
5 File formats
6 Games
7 Miscellaneous
8 System admin
9 Kernel routines

•We can enter the man command to display corresponding manual pages
as follows:
$ man 1 command
$ man 5 command
############################################################

mohammedrafi@NOC-RAFI:~$ whatis passwd
passwd (1ssl) – compute password hashes
passwd (1) – change user password
passwd (5) – the password file

mohammedrafi@NOC-RAFI:~$ whatis lsblk
lsblk (8) – list block devices

mohammedrafi@NOC-RAFI:~$ which passwd
/usr/bin/passwd

mohammedrafi@NOC-RAFI:~$ whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz /usr/share/man/man5/passwd.5.gz

############################################################

Some thing about aliases

mohammedrafi@NOC-RAFI:~$ alias
alias alert=’notify-send –urgency=low -i “$([ $? = 0 ] && echo terminal || echo error)” “$(history|tail -n1|sed -e ‘\”s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//’\”)”‘
alias egrep=’egrep –color=auto’
alias fgrep=’fgrep –color=auto’
alias grep=’grep –color=auto’
alias l=’ls -CF’
alias la=’ls -A’
alias ll=’ls -alF’
alias ls=’ls –color=auto’

mohammedrafi@NOC-RAFI:~$ alias mshaik=’date’

mohammedrafi@NOC-RAFI:~$ mshaik
Wed Jul 5 09:47:28 IST 2017

mohammedrafi@NOC-RAFI:~$ alias |grep mshaik
alias mshaik=’date’

But that is stick to that only that particular session i mean once u log out of that tty it’s no more to make it permanent follow below steps

mohammedrafi@NOC-RAFI:~$ alias |grep mshaik

mohammedrafi@NOC-RAFI:~$ cat ~/.bash_aliases
cat: /home/mohammedrafi/.bash_aliases: No such file or directory

mohammedrafi@NOC-RAFI:~$ touch ~/.bash_aliases

mohammedrafi@NOC-RAFI:~$ vim ~/.bash_aliases
alias mshaik=’date’

mohammedrafi@NOC-RAFI:~$ mshaik
mshaik: command not found

Make sure below lines exists if not u can safely insert those lines
mohammedrafi@NOC-RAFI:~$ vim ~/.bashrc
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

And u need to load that bashrc with source command

mohammedrafi@NOC-RAFI:~$ source ~/.bashrc
mohammedrafi@NOC-RAFI:~$ mshaik
Wed Jul 5 09:53:43 IST 2017

############################################################

enter image description here

mohammedrafi@NOC-RAFI:~$ touch originalfile

mohammedrafi@NOC-RAFI:~$ cat >>originalfile
Hai this is to test soft link

mohammedrafi@NOC-RAFI:~$ ls -li originalfile
28844772 -rw-rw-r– 1 mohammedrafi mohammedrafi 31 Jul 5 11:13 originalfile

mohammedrafi@NOC-RAFI:~$ cat originalfile
Hai this is to test soft link

mohammedrafi@NOC-RAFI:~$ ln -s originalfile symlink_of_originalfile

mohammedrafi@NOC-RAFI:~$ cat symlink_of_originalfile
Hai this is to test soft link

mohammedrafi@NOC-RAFI:~$ ls -li symlink_of_originalfile
28844775 lrwxrwxrwx 1 mohammedrafi mohammedrafi 12 Jul 5 11:15 symlink_of_originalfile -> originalfile

mohammedrafi@NOC-RAFI:~$ rm -rf originalfile

mohammedrafi@NOC-RAFI:~$ cat symlink_of_originalfile
cat: symlink_of_originalfile: No such file or directory

Soft Link is actual link to the original file. These Links will have a different Inodes value. Soft link points to the original file so if original file is deleted then the soft link fails. If you delete the Soft Link, nothing will happen to file. The reason for this is, the actual file or directory’s inode is different from the “soft link” created file’s inodes. Hard links can cross file systems.

##########################################
mohammedrafi@NOC-RAFI:~$ touch originalfile

mohammedrafi@NOC-RAFI:~$ cat >> originalfile
this is to test hard link

mohammedrafi@NOC-RAFI:~$ ln originalfile hardlink_for_originalfile

mohammedrafi@NOC-RAFI:~$ ls -li originalfile
28844776 -rw-rw-r– 2 mohammedrafi mohammedrafi 51 Jul 5 11:19 originalfile

mohammedrafi@NOC-RAFI:~$ ls -l hardlink_for_originalfile
-rw-rw-r– 2 mohammedrafi mohammedrafi 51 Jul 5 11:19 hardlink_for_originalfile

mohammedrafi@NOC-RAFI:~$ ls -li hardlink_for_originalfile
28844776 -rw-rw-r– 2 mohammedrafi mohammedrafi 51 Jul 5 11:19 hardlink_for_originalfile

mohammedrafi@NOC-RAFI:~$ cat hardlink_for_originalfile
this is to test hard link

Hard Link acts like a mirror copy of the original file. These links share the same inodes. Changes made to the original or hard linked file will reflect in the other. When you delete Hard Link nothing will happen to the other file. Hard links can’t cross file systems.

U can refer below link for more details

https://rafishaikblog.wordpress.com/2016/10/14/hard-soft-links-for-files-and-directories/

##########################################

To learn about the type of file, you can use the command file. In Linux,various types of files exist. Some examples are as follows:
° ° Regular file (-)
° ° Directory (d)
° ° Soft link (l)
° ° Character device driver (c)
° ° Block device driver (b)
° ° Pipe file (p)
° ° Socket file (s)

Setuid
Another very interesting functionality is the setuid feature. If the setuid bit is set for a script or file, then the script or file will always run with the owner’s privileges irrespective of which user is running the script. If the administrator wants to run script written by him by other users, then he can set this bit.

Setgid
Similar to setuid , the setgid functionality gives the user the ability to run scripts with group owner’s privileges, even if it is executed by any other user.

Sticky bit
Sticky bit is a very interesting functionality. Let’s say, in the administration department there are 10 users. If one folder has been set with sticky bit, then all other users can copy files to that folder. All users can read the files, but only the owner of the respective file can edit or delete the file. Other user can only read but not edit or modify the files if the sticky bit is set.

Introducing process basics
A running instance of a program is called as process. A program stored in the hard disk or pen drive is not a process. When that stored program starts executing, then we say that process has been created and is running. Let’s very briefly understand the Linux operating system boot-up sequence:

1.In PCs, initially the BIOS chip initializes system hardware, such as PCI bus, display device drivers, and so on.
2.Then the BIOS executes the boot loader program.
3.The boot loader program then copies kernel in memory, and after basic checks, it calls a kernel function called start_kenel() .
4.The kernel then initiates the OS and creates the first process called init .
5.You can check the presence of this process with the following command:
$ ps –ef
6.Every process in the OS has one numerical identification associated with it. It is called a process ID. The process ID of the init process is 1. This process is the parent process of all user space processes.
7.In the OS, every new process is created by a system call called fork() .
8.Therefore, every process has a process ID as well as the parent process ID.
9.We can see the complete process tree using the following command:
    $ pstree
You can see the very first process as init as well as all other processes with a complete parent and child relation between them. If we use the $ps –ef command,
then we can see that the init process is owned by root and its parent process ID is 0.

Know few basics commands

https://rafishaikblog.wordpress.com/2017/07/06/diffcutheadtailmorelesspast/

https://rafishaikblog.wordpress.com/2017/07/06/file-descriptors/

Checking and disabling shell internal commands

mohammedrafi@NOC-RAFI:~$ type ls
ls is aliased to `ls –color=auto’

This will make aliases and functions be ignored and the external ls command will execute.
mohammedrafi@NOC-RAFI:~$ command ls

To display all shell built-in commands, give the command as follows:
mohammedrafi@NOC-RAFI:~$ enable
enable dirs
enable disown
enable echo
enable enable
enable eval
enable exec
enable exit
enable export
enable false
enable fc
enable fg
enable getopts
enable hash
enable help
enable history
enable jobs
enable ulimit
enable umask
enable unalias
enable unset
enable wait

The exit status
mohammedrafi@NOC-RAFI:~$ ls

mohammedrafi@NOC-RAFI:~$ echo $?
0

mohammedrafi@NOC-RAFI:~$ ls /root/
ls: cannot open directory /root/: Permission denied

mohammedrafi@NOC-RAFI:~$ echo $?
2

Command substitution

mohammedrafi@NOC-RAFI:~$ echo “Hello `whoami` Good maorning hava a nice day”
Hello mohammedrafi Good maorning hava a nice day

mohammedrafi@NOC-RAFI:~$ echo “Hello $(who am i)”
Hello mohammedrafi pts/1 2017-07-06 14:38 (:0)

mohammedrafi@NOC-RAFI:~$ dirname=”$(pwd)”
mohammedrafi@NOC-RAFI:~$ echo $dirname
/home/mohammedrafi

mohammedrafi@NOC-RAFI:~$ dirname=”$(basename $(pwd))”
mohammedrafi@NOC-RAFI:~$ echo $dirname
mohammedrafi

Command grouping
mohammedrafi@NOC-RAFI:~$ date & cal
[1] 17095
Thu Jul 6 15:48:37 IST 2017
July 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
[1]+ Done date

The first command is started in the background to continue until it has finished; immediately after starting first command, the second command is started and it will run in the foreground:

Command grouping
mohammedrafi@NOC-RAFI:~$ date & cal
[1] 17095
Thu Jul 6 15:48:37 IST 2017
July 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
[1]+ Done date

The first command is started in the background to continue until it has finished; immediately after starting first command, the second command is started and it will run in the foreground:

mohammedrafi@NOC-RAFI:~$ pwd && date
/home/mohammedrafi
Thu Jul 6 15:52:42 IST 2017

The second command is only started if the first command is successful. To achieve this, the shell checks the exit (return) status of the first command and starts the second command only if and when that exit status is found to be “0”.

mohammedrafi@NOC-RAFI:~$ cat /etc/shadow && date
cat: /etc/shadow: Permission denied

mohammedrafi@NOC-RAFI:~$ cat /etc/shadow || date
cat: /etc/shadow: Permission denied
Thu Jul 6 15:54:34 IST 2017

The second command is only started if the first command fails. The shell checks the exit status of the first command and starts the second command only if that exit status is not equal to “0”.

If we give the command as follows:
$ cd /home/student/work/temp/; rm –rf *
Initially, the shell will change to the /home/student/work/temp folder, and then it will delete all files and folders.

If we give the command as follows:
$ cd /backup/ol/home/student/work/temp/ && rm * -rf

This will first change to the required folder, and then the rm command will be called for deletion. The problem with ” ; ” is that even if the shell fails to change to the required folder, the rm command will execute and it will delete all the files and folders from your original folder. This will be really dangerous.

#######################################################################

Understanding variables

mohammedrafi@NOC-RAFI:~$ name=jack

mohammedrafi@NOC-RAFI:~$ echo $name
jack

mohammedrafi@NOC-RAFI:~$ unset name

mohammedrafi@NOC-RAFI:~$ echo $name

mohammedrafi@NOC-RAFI:~$ name=jones
mohammedrafi@NOC-RAFI:~$ echo $name
jones
mohammedrafi@NOC-RAFI:~$ set |grep -w “jones”
name=jones
mohammedrafi@NOC-RAFI:~$ unset name
mohammedrafi@NOC-RAFI:~$ set |grep -w “jones”

Here, the env command will display all environmental variables.
mohammedrafi@NOC-RAFI:~$ env

mohammedrafi@NOC-RAFI:~$ USER1=john
mohammedrafi@NOC-RAFI:~$ env |grep john
mohammedrafi@NOC-RAFI:~$ export USER1
mohammedrafi@NOC-RAFI:~$ env |grep john
USER1=john
mohammedrafi@NOC-RAFI:~$ echo $USER1
john

mohammedrafi@NOC-RAFI:~$ grep USER1=john /home/mohammedrafi/.bashrc
mohammedrafi@NOC-RAFI:~$ grep USER1=john /home/mohammedrafi/.profile
mohammedrafi@NOC-RAFI:~$ grep USER1=john /etc/environment

mohammedrafi@NOC-RAFI:~$ echo “USER1=john” >>~/.bashrc
mohammedrafi@NOC-RAFI:~$ echo “USER1=john” >>~/.profile
mohammedrafi@NOC-RAFI:~$ sudo -i
root@NOC-RAFI:~# echo “USER1=john” >> /etc/environment

#!/bin/bash
echo “Enter variable name: “
read variable_name
echo “Enter variable value: “
read variable_value
echo “adding ” $variable_name ” to einvironment variables: ” $variable_value
echo “export “$variable_name”=”$variable_value>>~/.bashrc
echo $variable_name”=”$variable_value>>~/.profile
echo $variable_name”=”$variable_value>>/etc/environment
source ~/.bashrc
source ~/.profile
echo “do you wanna restart your computer to apply changes in /etc/environment file? yes(y)no(n)”
read restart
case $restart in
y) sudo shutdown -r 0;;
n) echo “don’t forget to restart your computer manually”;;
esac
exit

#######################################################################

Working with command line arguments

$0 Shell script name or command
$1–$9 Positional parameters 1–9
${10} Positional parameter 10
$# Total number of parameters
$* Evaluates to all the positional parameters
$@ Same as $*, except when double quoted
“$*” Displays all parameters as “$1 $2 $3”, and so on
“$@” Displays all parameters as “$1” “$2” “$3”, and so on

Understanding set

mohammedrafi@NOC-RAFI:~$ set USA Canada UK France
mohammedrafi@NOC-RAFI:~$ echo $0
bash
mohammedrafi@NOC-RAFI:~$ echo $1
USA
mohammedrafi@NOC-RAFI:~$ echo $2
Canada
mohammedrafi@NOC-RAFI:~$ echo $3
UK
mohammedrafi@NOC-RAFI:~$ echo $4
France
mohammedrafi@NOC-RAFI:~$ echo $5

mohammedrafi@NOC-RAFI:~$ echo $#
4
mohammedrafi@NOC-RAFI:~$ echo $*
USA Canada UK France
mohammedrafi@NOC-RAFI:~$ echo $@
USA Canada UK France
mohammedrafi@NOC-RAFI:~$ echo “$@”
USA Canada UK France
mohammedrafi@NOC-RAFI:~$ echo “$*”
USA Canada UK France

########################################################################

Understanding getopts

Command line parameters passed along with commands are also called as positional parameters.

mohammedrafi@NOC-RAFI:~$ vim one.sh

#!/bin/bash
USAGE=”usage: $0 -x -y”
while getopts :xy: opt_char
do
case $opt_char in
x)
echo “Option x was called.”
;;
y)
echo “Option y was called. Argument called is $OPTARG”
;;
\?)
echo “$OPTARG is not a valid option.”
echo “$USAGE”
;;
esac
done

mohammedrafi@NOC-RAFI:~$ chmod +x one.sh

mohammedrafi@NOC-RAFI:~$ sh one.sh -x
Option x was called.

mohammedrafi@NOC-RAFI:~$ sh one.sh -y status_check.sh
Option y was called. Argument called is status_check.sh

mohammedrafi@NOC-RAFI:~$ sh one.sh -y status_check.sh -x
Option y was called. Argument called is status_check.sh
Option x was called.

mohammedrafi@NOC-RAFI:~$ sh one.sh -z
z is not a valid option.
usage: one.sh -x -y

########################################################################

Understanding default parameters

Many times we may pass certain parameters from the command line; sometimes, we may not pass any parameters at all. We may need to have certain default values to be initialized to certain variables.

We will understand this concept by the following script.
Create a default_argument_1.sh script as follows:
#!/bin/bash
MY_PARAM=${1:-default}
echo $MY_PARAM

Execute the script and check:
$ chmod +x default_argument_1.sh One
$ ./default_argument_1.sh One
One

$ ./default_argument_1.sh
default
————————
Create another default_argument_2.sh script:
#!/bin/bash
variable1=$1
variable2=${2:-$variable1}
echo $variable1
echo $variable2

$ ./default_argument_1.sh One Two
One
Two

$ ./default_argument_1.sh One
One
One

##################################################################

mohammedrafi@NOC-RAFI:~$ FRUITS=(Apple Banana Mango)
mohammedrafi@NOC-RAFI:~$ echo ${FRUITS[*]}
Apple Banana Mango
mohammedrafi@NOC-RAFI:~$ echo ${FRUITS[2]}
Mango
mohammedrafi@NOC-RAFI:~$ echo $FRUITS[*]
Apple[*]

mohammedrafi@NOC-RAFI:~$ declare -a fruit=( Pears Apple Mango Banana Papaya )
mohammedrafi@NOC-RAFI:~$ echo ${fruit[0]}
Pears

#########################################################################

mohammedrafi@NOC-RAFI:~$ cat << HERE >>sample
hai this is to test HERE
and this is done
HERE

mohammedrafi@NOC-RAFI:~$ cat sample
hai this is to test HERE
and this is done

Understanding the opening, writing, and closing of a file
mohammedrafi@NOC-RAFI:~$ vim fd.sh
#!/bin/bash
exec 3> sample_out.txt
echo “This is a test message for sample_out.txt file” >&3
date >&3
exec 3<&-

mohammedrafi@NOC-RAFI:~$ cat sample_out.txt
This is a test message for sample_out.txt file
Sun Jul 9 09:33:43 IST 2017

Understanding reading from a file

mohammedrafi@NOC-RAFI:~$ vim fd1.sh
#!/bin/bash
# We will open file sample_input.txt for reading purpose.
# We are assigning descriptor 3 to the file.
exec 3< sample_input.txt
cat <&3
# Closing file
exec 3<&- mohammedrafi@NOC-RAFI:~$ echo “Hello to All” > sample_input.txt

mohammedrafi@NOC-RAFI:~$ sh fd1.sh
Hello to All

Understanding reading and writing to a file

mohammedrafi@NOC-RAFI:~$ vim fd3.sh
#!/bin/bash
file_name=”sample_out.txt”
# We are assing fd number 3 to file.
# We will be doing read and write operations on file
exec 3<> $file_name
# Writing to file
echo “””
Do not dwell in the past,
do not dream of the future,
concentrate the mind on the present moment. – Buddha
“”” >&3
# closing file with fd number 3
exec 3>&-

mohammedrafi@NOC-RAFI:~$ sh fd3.sh

mohammedrafi@NOC-RAFI:~$ cat sample_out.txt
Do not dwell in the past,
do not dream of the future,
concentrate the mind on the present moment. – Buddha

Summarizing usage of the exec command
exec command This command will replace shell and execute it. Therefore, it
will not return to its original shell, which started it.
exec > data.txt This opens data.txt for writing standard output.
exec < data.txt This opens data.txt for reading standard input.
exec 3< data.txt This opens data.txt for reading with descriptor 3.
sort <&3 This will sort data.txt file exec 4>data.txt This opens data.txt for writing with descriptor 4.
ll >&4 The output of ll is redirected to data.txt.
exec 6<&5 This makes fd 6 a copy of fd 5
exec 4<&– This closes fd 4

set command
mohammedrafi@NOC-RAFI:~$ vim set.sh
#!/bin/bash
str1=”USA”
str2=”Canada”;
[ $str1 = $str2 ]
echo $?
Set –x
[ $str1 != $str2 ]
echo $?
[ -z $str1 ]
echo $?
Set +x
[ -n $str2 ]
echo $?
Exit 0

mohammedrafi@NOC-RAFI:~$ sh set.sh
1
0
1
0

-n to check the length of string grater than zero
-z to check the string is empty or not

Test operator Tests true if
-n string True if the length of string is nonzero.
-z string True if the length of string is zero.

set -f Disables globbing. In this case, the file name expansions using wildcards or metacharacters will be disabled.
set -v This will print the Shell script lines as they are read by the shell.
set -x This option will display each line after the variable substitution and command expansion, but before execution by the shell. This option is often called shell tracing.
set -n This reads all commands and checks the syntax, but does not execute them.

Listing integers

mohammedrafi@NOC-RAFI:~$ declare -i
declare -ir BASHPID
declare -ir EUID=”1000″
declare -i HISTCMD
declare -i LINENO
declare -i MAILCHECK=”60″
declare -i OPTIND=”1″
declare -ir PPID=”19226″
declare -i RANDOM
declare -ir UID=”1000″

mohammedrafi@NOC-RAFI:~$ help let
let: let arg [arg …]
Evaluate arithmetic expressions.

Evaluate each ARG as an arithmetic expression. Evaluation is done in
fixed-width integers with no check for overflow, though division by 0
is trapped and flagged as an error. The following list of operators is
grouped into levels of equal-precedence operators. The levels are listed
in order of decreasing precedence.

id++, id– variable post-increment, post-decrement
++id, –id variable pre-increment, pre-decrement
-, + unary minus, plus
!, ~ logical and bitwise negation
** exponentiation
*, /, % multiplication, division, remainder
+, – addition, subtraction
<<, >> left and right bitwise shifts
<=, >=, <, > comparison
==, != equality, inequality
& bitwise AND
^ bitwise XOR
| bitwise OR
&& logical AND
|| logical OR
expr ? expr : expr
conditional operator
=, *=, /=, %=,
+=, -=, <<=, >>=,
&=, ^=, |= assignment

Shell variables are allowed as operands. The name of the variable
is replaced by its value (coerced to a fixed-width integer) within
an expression. The variable need not have its integer attribute
turned on to be used in an expression.

Operators are evaluated in order of precedence. Sub-expressions in
parentheses are evaluated first and may override the precedence
rules above.

Exit Status:
If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s