LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   same shell for getting input from user,command line and files. (https://www.linuxquestions.org/questions/linux-newbie-8/same-shell-for-getting-input-from-user-command-line-and-files-4175512110/)

packirisamy 07-24-2014 02:39 AM

same shell for getting input from user,command line and files.
 
My program is

read a
read b
read c
echo $a
echo $b
echo $c

when i run it as ./shell.sh
it gets input from user and prints it.

when i run it as ./shell.sh < file.txt
it takes input from file.txt and assigns it to variables a,b and c.

when i run as ./shell.sh 1 2 3
i want the arguments to be stored in a,b and c
without any change of code...
CAN SOMEONE HELP ME SOON???

jonnybinthemix 07-24-2014 03:54 AM

There's probably a more elegant way of doing it, but this sort of thing should work:

Code:

a=$1
b=$2
c=$3

if [ $# -eq 0 ]; then

read a
read b
read c

echo $a
echo $b
echo $c

else

echo $a
echo $b
echo $c

fi


packirisamy 07-24-2014 06:40 AM

jonnybinthemix sir... thank you so much for your quick response...


All times are GMT -5. The time now is 10:38 PM.