Monday, November 21, 2016

Material



http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_01.html


When to use symbols: {}, [], $, @, ., >, >>,"",'',


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

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

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





date - usage in shell


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

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

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.)
vityped at the command line to open one or more files in the same directory
(vi tomato.txt   opens a file named "tomato.txt" in the current directory)
(vi parsley sage rosemary   opens the three files "parsley," "sage" and "rosemary" in the current directory)
vi *typed at the command line to open every file in the current directory
:qcloses (quits) a file to which no changes have been made
:q!quits without saving any changes
:wwrites (i.e., saves) the current file to disk
:wqwrites the buffer contents to disk (i.e., saves changes) and quits
ZZsame as :wq
iactivates text insert mode, inserting text immediately under the current position of the cursor
hmoves the cursor one character to the left
(2h   moves the cursor two characters to the left)
jmoves the cursor one line down
(3j   moves the cursor three lines down)
kmoves the cursor one line up
lmoves the cursor one character to the right
Gmoves the cursor to the desired line; moves the cursor to the last line of text if not preceded by a modifying integer
(5G   moves the cursor to the fifth line)
aswitches to insert mode and allows insertion of text immediately to the right of the cursor
xdeletes the character immediately under the cursor
(xxx   deletes the character immediately under cursor and then deletes the two characters to its right)
Xdeletes a single character to the left of cursor
Dremoves the text on the current line from the character under the cursor to the end of the line
dwdeletes the character immediately under the cursor and the remaining characters to the right of it in the same word
(2dw   deletes the character immediately under the cursor, the remaining characters to the right of it in same word and all of the next word)
dddeletes the entire line containing the cursor, and the cursor then moves to the next line
(2dd   deletes two consecutive lines beginning with the current line)
cwdeletes the character under the cursor and to its right in the same word and allows new characters to be typed in to replace them
(2cw   deletes the character under the cursor and to its right in the same word and in the next word, and then allows replacement characters to be typed in)
ccerases the current line and allows replacement text to be typed in
(2cc   erases the current line and the next line and allows replacement text to be typed in for both lines)
cbdeletes the characters to the left of the cursor in the current word and allows replacement characters to be typed in
(3cb   deletes the characters to the left of the cursor in the current word together with the two words to its left and then allows replacement text to be typed in)
Ractivates text input mode allowing text under and to the right of the cursor to be overwritten one character at a time
xptransposes two adjacent characters
deeptransposes two adjacent words
ddptransposes two adjacent lines
~changes case of the character under the cursor
Jjoins the current line with the next line
ureverses the effects of the most recent command that has changed the buffer
Uundoes all changes made to the current line during the current visit to it
:s/searches the text for the first instance of a designated string
(:s/cucumber   searches the text for the first instance of the string "cucumber")
nsearches the text for the next instance of a designated string
:s/ / /replaces the first instance of a designated string
(:s/cucumber/radish/   replaces the first instance of the string "cucumber" with the string "radish")
:%s/ / /replaces every instance of a designated string
(:%s/cucumber/radish/   replaces every instance of the string "cucumber" with the string "radish")
:rinserts text into the currently open file from another file
(:r lettuce.txt   inserts text into the currently open file from the file named "lettuce.txt")
:w>>appends the text from the currently open file into another file
(:w>> cabbage   appends the text from the currently open file into the file named "cabbage")

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

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

Run setup-x86_64.exe any time you want to update or install a Cygwin package for 64-bit windows. The signature for setup-x86_64.exe can be used to verify the validity of this binary using this public key.