Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you ask, "Are you sure?"? 2

Status
Not open for further replies.

mwesticle

Programmer
Nov 19, 2003
51
US
I have a Korn Shell script that works based on one input parameter. The user passes one string value to the script from the command line, like this:

script.ksh <string>

The script will list all files that match that string.
Then it will move files that contain that string into a directory for later archival. Example:

script.ksh *test*

will output this to the screen:

test.txt
test1.txt
job1.test

Then it will move those files to another directory.

MY QUESTION: HOW CAN I GET THE SCRIPT TO ASK THE USER IF THEY'RE SURE THEY WANT TO DO THIS, AFTER SEEING THE FILES THAT WILL BE MOVED? Currently, it just shows the list and moves the files. I need the user to verify the list is correct before moving on, or stop if the list is incorrect. I'd like the user to just have to type 'Y' or 'N'. Is there a way to do this in a Korn Shell Script? Any help would be awesome! Thanks!
 
man read

if on Solaris,
man ckyorn

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Try something like this :

[tt]
# List files to move

echo &quot;Files to move :&quot;
for file
do
echo $file
done

# ask for confirmation

typeset -u Confirm
until [[ &quot;$Confirm&quot; = [YN] ]]
do
read Confirm?&quot;Are you sure [Y/N] ? &quot;
done

# Moves not confirmed

[ &quot;$Confirm&quot; = &quot;N&quot; ] && exit

# Files can be moved

for file
do
mv $file Save_Dir/
done
[/tt]



Jean Pierre.
 
Hi:

Here's a ckyorn replacement:

if ! type ckyorn > /dev/null
then
# this function emulates the Solaris internal ckyorn function.
# optional parameters:
# d - default value
# p - user prompt
# Q - disable q for quit
# return: Y, y, N, N and q, Q if Q option is not disabled.
function ckyorn {
typeset option=
typeset default=
typeset prompt=
typeset REPLY=
typeset UREPLY=
typeset Q=Qq

while getopts :p:d:Q option
do
case $option
in
d) default=$OPTARG ;;
p) prompt=$OPTARG ;;
Q) typeset Q=&quot;&quot; ;;
esac
done

while echo &quot;$prompt\c&quot; 1>&2
do
read REPLY
UREPLY=$REPLY
case &quot;${UREPLY:=$default}&quot;
in
[ynYN$Q]*) break ;;
*) echo &quot;ERROR: Please enter yes or no.&quot; 1>&2 ;;
esac
done

echo &quot;$UREPLY&quot;
}
fi

#example
default=y
prompt=&quot;Continue (y/n)? [$default]:&quot;
answer=$(ckyorn -Q -p &quot;$prompt&quot; -d &quot;$default&quot;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top