Is it possible to prompt for input if not given on command line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is it possible to prompt for input if not given on command line?
# 1  
Old 04-25-2012
Is it possible to prompt for input if not given on command line?

I have a script built that takes the standard inputs $1 $2 $3 after the name and parses some data.

Code:
 
hexsite=`echo "obase=16;$1"|bc`
hexfix=$(printf "%.3X" 0x$hexsite)
if [ "$2" == "1x" ] || [ "$2" == "1X" ];then
type=33
elif [ "$2" == "ev" ] || [ "$2" == "EV" ];then
type=59
elif [ "$2" == "both" ];then
type=99
else type=00
fi
cat /directory/*.2012$3*|

I am looking for a way to prompt for the input if someone does not include it in the command line. I can get the input via 'read' statements:

Code:
 
echo "Input site number: "
read decsite
hexsite=`echo "obase=16;$decsite"|bc`
hexfix=$(printf "%.3X" 0x$hexsite)
echo "Input type: "
read rawtype
if [ "$rawtype" == "1x" ] || [ "$rawtype" == "1X" ];then
type=33
elif [ "$rawtype" == "ev" ] || [ "$rawtype" == "EV" ];then
type=59
elif [ "$rawtype" == "both" ];then
type=99
else type=00
fi
echo "Input date: "
read daterange
cat /directory/*.2012$daterange*|

But I would like to work both ways instead of one or the other depending on what the user does.
# 2  
Old 04-25-2012
Test $# (number of args) and prompt/assign from args as required:

Code:
if [ $# -eq 2 ]
then
    decsite=$1
    rawtype=$2
else
    echo "Input site number: "
    read decsite
    echo "Input type: "
    read rawtype
fi

---------- Post updated at 08:30 AM ---------- Previous update was at 08:27 AM ----------

Think about using case for your rawtype test:

Code:
case $rawtype in
    1x|1X) type=33 ;;
    [eE][Vv]) type=59 ;;
    both|Both|BOTH) type=99 ;;
    *) type=100 ;;
esac

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 04-25-2012
How about:

Code:
while [ -z "${decsite:=$1}" ]; do
  printf "Input site number: "
  read decsite
done

while [ -z "${rawtype:=$2}" ]; do
  printf "Input type: "
  read rawtype
done

while [ -z "${daterange:=$3}" ]; do
  printf "Input date: "
  read daterange
done

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-25-2012
Awesome response, case is working great so far but the test fails. I forgot to mention I am using bash, sorry about that.

I give it command line entries and it still prompts and overwrites the variables at the moment. I am back to digging with this guidance though, so it is good. Smilie

I will look up the while option too, since it never hurts to know more than one way to skin a <insert animal or vegetable of choice here>!

Last edited by PCGameGuy; 04-25-2012 at 07:54 PM..
# 5  
Old 04-25-2012
Reason the test failed is that I missed that you have 3 params (not 2).

Scrutinizer's solution is pretty nice as it also traps blank values being entered when prompting.
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 04-25-2012
Quote:
Originally Posted by Chubler_XL
Reason the test failed is that I missed that you have 3 params (not 2).

Scrutinizer's solution is pretty nice as it also traps blank values being entered when prompting.
Oh, I added my third variable when trying it. Is it that $# won't trap all three? I take it using - eq 2 checks for existence?

Code:
 
if [ $# -eq 2 ]
then
decsite=$1
rawtype=$2
daterange=$3
else
echo "Input site number: "
read decsite
echo "Input type: "
read rawtype
echo "Input date: "
read daterange
fi

---------- Post updated at 04:14 PM ---------- Previous update was at 04:06 PM ----------

Scrutinizer's answer worked beautifully! I have NO idea how and will spend the next hours/days figuring it out, but it works. Smilie

Chubler_XL, case is much more elegant in the script as well, so thanks for that!
# 7  
Old 04-25-2012
No $# is the number of params passed so you would want to check that 3 were passed with -eq 3 like this:

Code:
if [ $# -eq 3 ]
then
   decsite=$1
   rawtype=$2
   daterange=$3
else
   echo "Input site number: "
   read decsite
   echo "Input type: "
   read rawtype
   echo "Input date: "
   read daterange
fi

Schrutinizer's solution will prompt for daterange if you only pass 2 and rawtype+daterange if you only pass 1. This gives you a mix of passed and prompted values.
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command line multiple input

I'm using the below to get multiple input from USER and it is working, is there any better way in awk array single liner? echo "Enter Multiple input (Ctrl+d to exit)" >output while read A do echo "$A" >>output done (3 Replies)
Discussion started by: Roozo
3 Replies

2. Shell Programming and Scripting

Using redirect for input instead of command line

I have a script that is working when I input parameters from the command line. I was under the assumption that i could use a redirect for the input. script fred.sh: value1=$1 value2=$2 print $value1 $value2 Running from command line: fred.sh 1 2 1 2 Contents of file fred.txt:... (3 Replies)
Discussion started by: djm2112
3 Replies

3. UNIX for Advanced & Expert Users

Entire Input command not visible in Unix prompt

Hi, While typing the Unix command, entire command is not visible.When the input command is long, it is not visible. I want the entire command to be displayed when i type it. Please help to resolve this issue. Thanks Sampath (7 Replies)
Discussion started by: sampath.giri
7 Replies

4. Shell Programming and Scripting

Input variable in command line

i have this script that reads a file "listall_101111" where 101111 varies. awk '{print $0,$1}' listall_101111 | sed -e 's/\(.*\).$/\1/g' | awk '{print $6,$2,$3,$4,$5}' | sort -nrk5 | nawk 'NR==1{m=$5;a++;b=(b)?b:$0;next}$5==m{a++;b=(b)?b:$0}END{for (i in a){print b,a}}' | grep -v ^LG | sort... (4 Replies)
Discussion started by: aydj
4 Replies

5. Shell Programming and Scripting

Command line user input validation

Hi guys, I have a piece of snippet below which asks the user to input some numbers if isDatasubEnabled && isReconEnabled; then echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit" elif isDatasubEnabled &&... (4 Replies)
Discussion started by: pyscho
4 Replies

6. UNIX Desktop Questions & Answers

Website-Command Line Prompt

Hello guys... I am having a doubt. Please try to rectify it. I would really appreciate it. The thing is that is it possible to open any website say for example,google from the command line prompt(terminal) if you are working in Linux-fedora... I am very new to Unix. regards, Mahesh... (2 Replies)
Discussion started by: mraghunandanan
2 Replies

7. UNIX for Advanced & Expert Users

Help-prompt for path and take this as input in find command

HI , I am trying to wite a script that will prompt me saying " what is path that you want to find ?". once i specify the path, the script should put this path in the find command mentioned below and execute the script: find <path> -ctime +200 -type f -exec ls -l {} \; for example : ... (7 Replies)
Discussion started by: bsandeep_80
7 Replies

8. Shell Programming and Scripting

How to enter if-then condition on command prompt/line ?

Hi I have some trouble entering if-then condition in a single line on a command prompt in csh. Could someone show how does one do that ? eg: source .cshrc; cd $dir; pwd; test -d $backup_dir; if then mkdir -p ${backup_dir}; echo inside loop; fi; echo outside loop; mv -f... (3 Replies)
Discussion started by: mpc8250
3 Replies

9. Shell Programming and Scripting

How to prompt for input & accept input in ONE line

hi, am a new learner to shell programming. i have a script which will prompt for user to key in their name & display their name afterwards. script ===== echo "Pls enter your name:" read name echo "Your name is $name." output ===== Pls enter your name: Bob Your name is Bob. what... (2 Replies)
Discussion started by: newbie168
2 Replies

10. Programming

using command line input in C code

Hi I need to list files by their size and exclude those that match a certain size. With grep it is easy enough. ll -rt | grep 2166 | awk '{print $9}' 2166 is the filesize and $9 the filenamefield. What should I be looking for to use this in a C program? My aim is to cut the name... (2 Replies)
Discussion started by: vannie
2 Replies
Login or Register to Ask a Question