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. :slight_smile:

#!/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"
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

Good first question, thanks.

scp requires two parameters:

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:

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

1 Like

Hi Sir Jim,

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

:):):slight_smile: