http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_01.html
Monday, November 21, 2016
Comments in Shell
Anything that follows the pound sign is ignored. If the pound sign starts at the beginning of a line, then entire line is ignored. If a pound sign is encountered in the middle of a line, only the information to the right of the pound sign is ignored.
Positional Parameters in shell script
In positional Parameter are variables that contain the contents of the command line. These variables are $0 to $9. The script itself is stored in $0, first parameter is $1 and so on. This is how parameters are passed to the script.
$ sh <scirpt_name>.sh parameter1 parameter2 ....
Ex:
echo "the script: $0 is passed with first parameter value: $1 and second parameter: $2"
dlyelgg@lgpbd1100[(IPC2-Gold-EDGE) ~/srik]$ sh Positional_Parameters.sh 1 2
$ sh <scirpt_name>.sh parameter1 parameter2 ....
Ex:
echo "the script: $0 is passed with first parameter value: $1 and second parameter: $2"
dlyelgg@lgpbd1100[(IPC2-Gold-EDGE) ~/srik]$ sh Positional_Parameters.sh 1 2
O/P:
the script: Positional_Parameters.sh is passed with first parameter value: 1 and second parameter: 2
-----------------------
To access all parameters in positional parameters use $@
Ex:
for para in $@
do
echo "The parameters passed to the script: $0 are $para"
done
-----------------------
To access all parameters in positional parameters use $@
Ex:
for para in $@
do
echo "The parameters passed to the script: $0 are $para"
done
dlyelgg@lgpbd1100[(IPC2-Gold-EDGE) ~/srik]$ sh Accessing_PossitionalParameters.sh 1 2 3
'ccessing_PossitionalParameters.sh: line 2: syntax error near unexpected token `do
'ccessing_PossitionalParameters.sh: line 2: `do
Operators in Shell
Arithmetic operators:
str1 -le str2 --less than or equal
str1 -ge str2 --- greater than or equal
str1 -ne str2 --- not equal
str1 -lt str2 --less than
str1 -gt str2 --- greater than
str1 -eq str2 --- equal to
#!/bin/bash
var1=1
var2=2
echo $var1
if ["${var1}"=="${var2}"];then
echo "Var1 has valid value"
fi
#if [$var1 -eq $var2]
#then
#echo "variable values are same"
#fi
dlyelgg@lgpbd1070[(IPC2-Gold-EDGE) ~/srik]$ sh Arithmetic_operators.sh
1
Arithmetic_operators.sh: line 12: syntax error: unexpected end of file
While Loop - Not working
https://www.cyberciti.biz/faq/shell-script-while-loop-examples/
c=1
While [ $c -le 5 ]
do
echo "Welcome $c times"
$c=$c+1
done
O/p:
Forloop_example.sh: line 5: While: command not found
: command not found line 6: do
timese 1
: command not found line 8: 1
: command not found line 9: done
c=1
While [ $c -le 5 ]
do
echo "Welcome $c times"
$c=$c+1
done
O/p:
Forloop_example.sh: line 5: While: command not found
: command not found line 6: do
timese 1
: command not found line 8: 1
: command not found line 9: done
For Loop -- not working
11/21/2016
url: http://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax
dlyelgg@lgpbd1070[(IPC2-Gold-EDGE) ~/srik]$ vi Forloop_example.sh
#Lists= $(ls *.sh)
for List in ${1...10}
do
echo "File name from the list: ${List}"
done
---------------------------
#!/bin/bash
#Lists= $(ls *.sh)
Lists= "red green yellow"
for List in $Lists
do
echo "File name from the list: $List"
done
O/P:
: command not found line 4: red green yellow
'orloop_example.sh: line 6: syntax error near unexpected token `do
'orloop_example.sh: line 6: `do
---------------------------
max=10
for (( i=2; i <= $max; ++i ))
do
echo "$i"
done
O/P:
syntax error near unexpected token `
'orloop_example.sh: line 6: `for (( i=2; i <= $max; ++i ))
-------------
for i in {2..10}
do
echo "output: $i"
done
for List in {1...10}
do
echo "File name from the list: $List"
done
o/p: syntax error near unexpected token `do
'orloop_example.sh: line 6: `do
-----------------------------------------
max=10
for i in `seq 2 $max`
do
echo "$i"
done
-----------
for i in {1..5}; do
echo $i
done
o/p:
syntax error near unexpected token `do
'orloop_example.sh: line 5: `for i in {1..5}; do
url: http://stackoverflow.com/questions/1445452/shell-script-for-loop-syntax
dlyelgg@lgpbd1070[(IPC2-Gold-EDGE) ~/srik]$ vi Forloop_example.sh
#Lists= $(ls *.sh)
for List in ${1...10}
do
echo "File name from the list: ${List}"
done
---------------------------
#!/bin/bash
#Lists= $(ls *.sh)
Lists= "red green yellow"
for List in $Lists
do
echo "File name from the list: $List"
done
O/P:
: command not found line 4: red green yellow
'orloop_example.sh: line 6: syntax error near unexpected token `do
'orloop_example.sh: line 6: `do
---------------------------
max=10
for (( i=2; i <= $max; ++i ))
do
echo "$i"
done
O/P:
syntax error near unexpected token `
'orloop_example.sh: line 6: `for (( i=2; i <= $max; ++i ))
-------------
for i in {2..10}
do
echo "output: $i"
done
for List in {1...10}
do
echo "File name from the list: $List"
done
o/p: syntax error near unexpected token `do
'orloop_example.sh: line 6: `do
-----------------------------------------
max=10
for i in `seq 2 $max`
do
echo "$i"
done
-----------
for i in {1..5}; do
echo $i
done
o/p:
syntax error near unexpected token `do
'orloop_example.sh: line 5: `for i in {1..5}; do
Friday, November 18, 2016
Putty - Shell file commands
Working on Files through Putty(or shell scripting)
Source: http://www.linfo.org/vi/summary.html
Summary of Commands
The following list contains the basic commands presented in the first eight pages of this tutorial along with occasional examples of usage (shown in parenthesis). They are presented in roughly the same order in which they appear in the tutorial. (All commands that begin with a colon are followed by ENTER.)
|
Tuesday, November 15, 2016
Strings and Variable comparisons ---- if loop
Source: http://stackoverflow.com/questions/20274099/comparing-variables-in-shell-scripts
11/15/2016:
Compare two variables:
#!/bin/bash #
echo "I am in"
var1="I am working on shell scripting"
var2="I am not working on shell script"
if [ "{$var1}" == "{$var2}" ] ; then
echo "$var1"
else
echo "$var2"
fi
dlyelgg@lgpbd1100[(IPC2-Gold-EDGE) ~/srik]$ sh if_statement1.sh
I am in
if_statement1.sh: line 10: syntax error: unexpected end of file
Compare variable with string:
#!/bin/bash #
echo "I am in"
var1="I am working on shell scripting"
var2="I am not working on shell script"
if [ "$var1" == "I am working on shell scripting" ] ; then
echo "$var1"
else
echo "$var2"
fi
sh first.sh
O/P:
I am in
I am working on shell scripting
Compare two Strings:
#!/bin/bash #
echo "I am in"
var1="I am working on shell scripting"
var2="I am not working on shell script"
if [ "I am not working on shell script" == "I am working on shell scripting" ] ; then
echo "$var2"
else
echo "$var1"
fi
sh first.sh
O/P:
I am in
I am working on shell scripting
O/P:
I am in
I am working on shell scripting
Tuesday, November 8, 2016
How to Run shell scripting on Windows
11/8/16
Cygwin is the environment that gives Linux feel on Windows.
Get that Linux feeling - on Windows
Source : https://cygwin.com/install.html
Installing and Updating Cygwin for 64-bit versions of Windows
Subscribe to:
Comments (Atom)