Script Inputs ( Arguments, Read, STDIN)

Example Script 1

[prabhucloudxlab@cxln4 ~]$ cat>hello.sh
#! bin/bash
echo $1 $2 $3
echo "================================="

echo $0 $1 $2 $3
echo "================================="

args=("$@")
echo ${args[0]} ${args[1]} ${args[2]}
echo "================================="


args=("$@")
echo $@
echo "length of the array is: $#"
echo "================================="





[prabhucloudxlab@cxln4 ~]$ sh hello.sh hari taraka prabhu
hari taraka prabhu
=================================
hello.sh hari taraka prabhu
=================================
hari taraka prabhu
=================================
hari taraka prabhu
length of the array is: 3
=================================

#Note: 
args[0]=hari, args[1]=taraka, args[2]=prabhu


=================================================================
Example Script 2


[prabhucloudxlab@cxl$n4 ~]$ cat>shell-2.sh

#! /bin/bash
#-----------READ USER INPUT

echo "enter the user name is:"
read name

echo "enter the height is:"
read height

echo "entered the user name is:$name"
sleep 2

echo "$name height is :$height"
sleep 2


prabhucloudxlab@cxl$n4 ~]$ sh shell-2.sh
enter the user name is:
PRABHU
enter the height is:
180CM
entered the user name is:PRABHU
PRABHU height is :180CM


=================================================================
Example Script 3



[prabhucloudxlab@cxl$n4 ~]$ cat>shell-3.sh

#! /bin/bash
#-----------ARGUMENTS

#Print total arguments and their values
echo "Total arguments:" $#
echo "All arguments values are:" $@

# command arguments can be accessed as
echo "first--->>" $1
echo "second--->>"  $2
echo "third--->>" $3
echo "fourth--->>" $4
city=$5
place=$6
echo "city is: $city and place is $place"



[prabhucloudxlab@cxl$n4 ~]$ sh shell-3.sh HARI TARAKA PRABHU MANYAM ANDHRAPRADESH WESTGODHAVARI
Total arguments: 6
All arguments values are: HARI TARAKA PRABHU MANYAM ANDHRAPRADESH WESTGODHAVARI

first--->> HARI
second--->> TARAKA
third--->> PRABHU
fourth--->> MANYAM

city is: ANDHRAPRADESH and place is WESTGODHAVARI


=================================================================
Example Script 4

[prabhucloudxlab@cxln4 ~]$ cat>hello.sh
#! bin/bash
# stdin file

while read line
do
        echo "$line"
done < "${1:-/dev/stdin}"


[prabhucloudxlab@cxln4 ~]$ sh hello.sh
hai this is hari
hai this is hari

#stdin will think terminal is a input file and assume its coming from input file. what ever you are typing in terminal it will take as input.


[prabhucloudxlab@cxln4 ~]$ sh hello.sh hello.txt
hello bash script
hello linux learners
i am going to add new lines in previous hello.txt file
check it output... Helloooooooooooo!
Adding the data for commetns section example...