Help Me. The script should not exit until the user gives an input.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Me. The script should not exit until the user gives an input.
# 1  
Old 08-10-2017
Help Me. The script should not exit until the user gives an input.

Hi everyone, I'm new here and just a beginner in linux scripting.

Just want to ask for help on this one.

I am trying to create a script that will accept user input (year-month and user/s).

I wanted to have the script to continue running, until the user inputs a DATE and name/s of user/s.

Using the script below, when you do not input anything, it will echo "Please enter your start date" and will end the script, forcing you to run the script again. Same with the end date and user/s.

Hope someone would help me.

Thank you very much. Smilie

Code:
#!/bin/bash

#This script is used to extract GWI Restored Materials within a month from specified users.
#Date range used should be in this format: YYYY-MM.
#Users input should be in this format: "'nabaguisa','ccbacong','pmvito'". Double quotes are important to accept the names.


DATE1=$1
DATE2=$2
USERS=$3
DIR=/root/Desktop/GWI_Restored_Materials

echo "Please input start date: [ YYYY-MM ], followed by [ENTER] "

        read DATE1

                if [ -z "$DATE1" ]; then
                        echo "Please enter your start date"

                else
echo " "
echo "Please input end date: [ YYYY-MM ], followed by [ENTER] "

        read DATE2

                if [ -z "$DATE2" ]; then
                        echo "Please enter your end date"

                else
echo " "
echo "Please input user/s: [ 'user1' ], followed by [ENTER] "
echo "Multiple users should be separated by a comma [,]. Ex: 'user1','user2','user3'...."

        read USERS

                if [ -z "$USERS" ]; then
                        echo "Please enter user/s"

                else
echo " "
echo "Extracting Materials from $DATE1 to $DATE2 by user/s $USERS"
Moderator's Comments:
Mod Comment Artificially truncated this long command line:
adt "select xfh_dest_min_id, xfh_source_filename, XFH_CREATE_BY, xfh_source_svc_handle as Source, xfh_dest_svc_handle as Destination, xfh_data_done/1024/1024 as filesize_MB, xfh_create_ts from ardome_xfer_history where xfh_source_svc_handle='tsm0' and XFH_CREATE_BY in ($USERS) and xfh_create_ts>'$DATE1-01 00:00:00' and xfh_create_ts<'$DATE2-01 00:00:00'" > $DIR/"$DATE1"_"$DATE2".txt echo " " echo "File has been exported to $DIR" echo " " echo "User armedia is copying file to NAS/Documentations/MonthlyReport_NEWSLA (password might be needed)" scp $DIR/"$DATE1"_"$DATE2".txt fi fi fi exec bash


Last edited by jim mcnamara; 08-10-2017 at 10:52 PM..
# 2  
Old 08-10-2017
Good first question, thanks.

scp requires two parameters:
Code:
scp sourcefile  remotecomputer:/path/to/destfile

Your problem is the if then read else fi blocks..

pseudocode this will not work for every shell, so be sure to check syntax for functions, for example:

Code:
function get()
{
    prompt="$1"
    value=""
    while [ ${#value} -eq 0 ]
    do
        echo -n $"$prompt  "
        read value
        if [ -z "$value" ] ; then
            echo "bad input" >2
            exit
        fi
        # optional check logic goes here, like is it a valid date or username
    done 
    # we get here because input is okay
    echo "$value"    
}

#usage example:
mydate=$( get "Enter a date")
myuser=$( get "Enter a list of users")
moredata=$(get "please enter more data or whatever")

create a function you call to check dates or whatever as you please. Then get rid of the long if ... then ..else.. fi code
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 08-10-2017
Hi Sir Jim,

Thank you for your reply. I will study and try this one.

SmilieSmilieSmilie

Quote:
Originally Posted by jim mcnamara
Good first question, thanks.

scp requires two parameters:
Code:
scp sourcefile  remotecomputer:/path/to/destfile

Your problem is the if then read else fi blocks..

pseudocode this will not work for every shell, so be sure to check syntax for functions, for example:

Code:
function get()
{
    prompt="$1"
    value=""
    while [ ${#value} -eq 0 ]
    do
        echo -n $"$prompt  "
        read value
        if [ -z "$value" ] ; then
            echo "bad input" >2
            exit
        fi
        # optional check logic goes here, like is it a valid date or username
    done 
    # we get here because input is okay
    echo "$value"    
}

#usage example:
mydate=$( get "Enter a date")
myuser=$( get "Enter a list of users")
moredata=$(get "please enter more data or whatever")

create a function you call to check dates or whatever as you please. Then get rid of the long if ... then ..else.. fi code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit script and open program via other user

Hello all.. so i have a problem i need to solve .. #! /bin/bash $SHELL dtterm -title my_prog -e su -user -c 'export DISPLAY=:0.0 ; /path/to/my/prog' & 2> /dev/null $SHELL intr exit This script will work on solaris 10 system in right clikt menu - in a secure system so i need to... (0 Replies)
Discussion started by: defs
0 Replies

2. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

3. Ubuntu

Exit user in bash script

I'm writing a bunch of scripts to automatically configure Ubuntu and I want to run the code below to remove the white dots from the login screen: sudo xhost +SI:localuser:lightdm sudo su lightdm -s /bin/bash gsettings set com.canonical.unity-greeter draw-grid false The problem is that... (3 Replies)
Discussion started by: maerlyngb
3 Replies

4. Shell Programming and Scripting

Create user if UID not exist; else, exit the script

Hi, I want to write a script to check whether an user ID is used in my server and then create that user. If the user ID is not used, I will echo something like "OK, continue" and then continue to execute the script. Else, I will echo something like "Used, exit" and then exit the script. As... (4 Replies)
Discussion started by: dirkaulo
4 Replies

5. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

6. Shell Programming and Scripting

How to take input from user or awk script?

Hi Jim, I have following script,i which i need to take dynamic value . script, nawk -v v1=grep"INT_EUR" $propertifilename | cut -d"=" -F2` -F'~' '{if (NF-1 !=v1) {print "Error in " $0 " at line number "NR" tilde count " N-1}}' $filename In the above script i want to use INT_EUR as a variable... (2 Replies)
Discussion started by: Ganesh Khandare
2 Replies

7. Shell Programming and Scripting

User Input Shell Script

Hello I am trying to create a user input shell scipt. The objective is user should enter the circuit number and the input is saved in a log file. If the user does not enter anything then the question should prompt it until the circuit no. is entered. Can any one please correct the code below.... (3 Replies)
Discussion started by: sureshcisco
3 Replies

8. Shell Programming and Scripting

Exit script if the user dosent enter any data within 5 seconds

Hello friends, Kindly help me in developing a script that asks user to enter a value and will wait for 5 seconds for the feedback. If there is no answer from the user the script will perform exit or it will continue doing something else Ex: If yu have a multi OS system i believe while... (3 Replies)
Discussion started by: frozensmilz
3 Replies

9. Shell Programming and Scripting

User input for execution of script

Hi, I need to get the user input and execute a particular script based on the input provided. For E.g. When I execute the script say Test.sh it should prompt "For which country I need to execute the script? (US/India)" Based on the input as US or India from the user the execution of... (8 Replies)
Discussion started by: yoursdavinder
8 Replies

10. Shell Programming and Scripting

exit script if user input not four characters

#!/usr/bin/bash ###script to input four characters. wxyz echo "input first string" read instring1 echo "input second string" read instring2 ## echo "first string is:" $instring1 echo "second string is:" $instring2 ##IF instring1 or instring2 are NOT 4 characters (xxxx) , exit 1. ##how?? ... (2 Replies)
Discussion started by: ajp7701
2 Replies
Login or Register to Ask a Question