LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-24-2014, 02:39 AM   #1
packirisamy
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Rep: Reputation: Disabled
Unhappy 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???
 
Old 07-24-2014, 03:54 AM   #2
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 07-24-2014, 06:40 AM   #3
packirisamy
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
jonnybinthemix sir... thank you so much for your quick response...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Backing up user files from the command line? skaldicpoet9 Ubuntu 4 07-17-2013 08:53 PM
Shell Script Error : too many arguments in input line bibhaw Programming 5 02-22-2012 10:32 AM
[Perl] Is there a way to encrypt command-line input when user enter a password ? android2009 Programming 2 11-05-2010 02:13 PM
(SOLVED) bash shell read user argument from command line and test it atnonis Programming 12 08-30-2010 07:11 AM
Using shell command output as input in shell script - how to do? EnderX Linux - Newbie 2 06-30-2010 12:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration