Prompt for 2 variables if nothing is entered end script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prompt for 2 variables if nothing is entered end script
# 1  
Old 03-24-2010
Error Prompt for 2 variables if nothing is entered end script

What I'm trying to do is write a bash file that prompts for the user's name and what the want to name the output file. If nothing is entered in either prompt the script must end and not create the output file. This is what I have so far:

Code:
#!/bin/sh
echo "What is your name?"
read NAME
if [ -s "$NAME" ]
    then
        break
    else
       echo "No name given"
       exit
fi
echo "What would you like to name your file?"
read FILE
if [ -s "$FILE" ]
    then
        break
    else
        echo "No script name"
        exit
fi
echo "#########################################################################$
#######
#This is CSI 135 Project 1 Question 2
# Output file: $FILE
# Created by: $NAME
# Data and time: `date`
###############################################################################$
#######
Hello, world\!\!
I am the script $FILE"
> $FILE

# 2  
Old 03-24-2010
Hello, try -n instead of -s
# 3  
Old 03-24-2010
Error Thank you

That worked the only problem I'm having now is when the person inputs the filename they want ... I need that file to actually be created so that I can redirect the output to that file .... if I still have the same code as above changing the -s to -n what else is needed?
# 4  
Old 03-25-2010
Code:
echo "What is your name?"
read NAME
[ "$NAME"  = "" ] && echo "No name given" >&2 && exit 1
 
echo "What would you like to name your file?"
read FILE
[  "$FILE" = "" ] && echo "No script name" >&2 && exit 2

cat <<EOF >$FILE
#########################################################################$
#######
#This is CSI 135 Project 1 Question 2
# Output file: $FILE
# Created by: $NAME
# Data and time: $(date)
###############################################################################$
#######
Hello, world !
I am the script $FILE
EOF

If you like to set FILE is stdout after read then
Code:
exec >$FILE
# and stdout is now some file

# 5  
Old 03-25-2010

Code:
#!/bin/sh

printf "%s " "What is your name?"
read NAME
if [ -z "$NAME" ]
then
   echo "No name given"
   exit 1
fi

printf "%s " "What would you like to name your file?"
read FILE
if [ -z "$FILE" ]
then
    echo "No script name"
    exit 1
fi

echo "#########################################################################
# This is CSI 135 Project 1 Question 2
# Output file: $FILE
# Created by: $NAME
# Data and time: `date`
#########################################################################
echo "Hello, world!!
I am the script $FILE" > "$FILE"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find between lines start and end: awk with bash variables

I have a file as follows: 0 1056 85540 414329 774485 1208487 1657519 2102753 2561259 3037737 3458144 3993019 4417959 4809964 5261890 5798778 6254146 I want to find all lines between a specified start and end tag. (6 Replies)
Discussion started by: jamie_123
6 Replies

2. Solaris

Unable to move from rsc prompt to ok prompt

Hi, on sunfire v890 unable to move from rsc prompt to ok prompt, i have executed the command break. (9 Replies)
Discussion started by: manoj.solaris
9 Replies

3. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

4. Shell Programming and Scripting

Shell script - entered input(1-40 bytes) needs to be converted exactly 40 bytes

hello, suppose, entered input is of 1-40 bytes, i need it to be converted to 40 bytes exactly. example: if i have entered my name anywhere between 1-40 i want it to be stored with 40 bytes exactly. enter your name: donald duck (this is of 11 bytes) expected is as below - display 11... (3 Replies)
Discussion started by: shravan.300
3 Replies

5. Shell Programming and Scripting

Delete empty files from a directory entered in command prompt

My code to "Delete empty files from a directory entered in command promt" #/bin/sh echo "Enter directory" read gh for file in `ls $gh` do # to get the size of file a=$( ls -l file | awk ' {print $7} '); echo $a if then echo "removing file " rm file fi done (6 Replies)
Discussion started by: adirajup
6 Replies

6. Shell Programming and Scripting

Use of Begin IF ,END IF END not working in the sql script

Hi I have written a script .The script runs properly if i write sql queries .But if i use PLSQL commands of BEGIN if end if , end ,then on running the script the comamds are getting printed on the prompt . Ex :temp.sql After connecting to the databse at the sql prompt i type... (1 Reply)
Discussion started by: isha_1
1 Replies

7. Shell Programming and Scripting

How to continue script if right word is not entered?

Hello, I am writing a script and in this script, I want to be able to have the script continue running if the correct word is not entered... Here is an excerpt from me script: read request if ; then echo "You have asked for the System Temperature..." cat... (1 Reply)
Discussion started by: niconico96
1 Replies

8. Shell Programming and Scripting

Getting cntrl-c entered inside a ksh script

hi guys, my ksh script is calling another script. The other script expects user to press CNTR-C, and does not return to the prompt. in my script, I want to call the other script, but somehow don't want it to wait forever, I want to return to my script. e.g. script2.ksh outputs: "No... (2 Replies)
Discussion started by: JamesByars
2 Replies

9. Shell Programming and Scripting

Command prompt from within a script?

I am writing a menu script and one of the options is to access a Command Prompt and return to the menu via CTRL-D. I have tried using a loop and echoing the PS1 value, then using read to assign whatever is entered into a variable, then executing the value of that variable as a command using .... (1 Reply)
Discussion started by: sanitywonko
1 Replies

10. UNIX for Dummies Questions & Answers

script returns prompt

Hi, I am trying to create a script that will loop through my oratab file and pull out the instance name. Here is script: for instance in $(cat /etc/oratab|egrep ':N|:Y'|grep -v \*|grep -v \#|cut -f1 -d':') do ... (3 Replies)
Discussion started by: kburrows
3 Replies
Login or Register to Ask a Question