[Solved] Use of until loop for user confirmation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Use of until loop for user confirmation
# 1  
Old 04-25-2012
[Solved] Use of until loop for user confirmation

Below is my script that is using to rename the name of file .Here I am using two methods to pass the both arguments wih script name or run the script and give the input one by one.But my issue is I want to rename the name of the file if user select Y(y) then it should rename the file else select N(n) then it should exit from the script.I am using until but I think I am doing something wrong.Can you help me on this where I am wrong

Code:
ConfY_N=Y
ConfY_N=N
## Prompt Arg4
##
prompt_IM_StartDt ()
{
 echo -e "\n\nUser will enter Start Time here : ";
 read IM_StartDt;
}
##
## Set Arg4
##
set_IM_StartDt ()
{
 ## Fetching Start Date for Product Classification folder.
 IM_StartDt="";
 if [[ -z "${IM_StartDt}" ]]; then prompt_IM_StartDt; fi
}
# ----------------------------------------------------------------------------------------------------------------
## Prompt Arg5
##
prompt_IM_CloseDt ()
{
 echo -e "\n\nUser will enter Start Time here : ";
 read IM_CloseDt;
}
##
## Set Arg5
##
set_IM_CloseDt ()
{
 ## Fetching Close Date for Product Classification folder.
 IM_CloseDt="";
 if [[ -z "${IM_CloseDt}" ]]; then prompt_IM_CloseDt; fi
## Give priority to passed parameters first.
IM_StartDt="${1}";
IM_CloseDt="${2}";

#copy ()
#{
#mv ${IM_StartDt} ${IM_CloseDt}
#}
## Check arguments and set them if reqd.
## -------------------------------------
for arg in IM_StartDt:${IM_StartDt} IM_CloseDt:${IM_CloseDt};
do
  ## Get Var/Val pair
  var="${arg%:*}";
  val="${arg#*:}";
  ## Check IM variable null condition. If not set, find it from Master config file. If Master config file doesn't store the value, prompt the user (last option).
  if [[ -z "${val}" ]]; then
     set_${var};
  fi
done
#copy
until [[ $ConfY_N == "Y" ]];
do
  echo -e "\n\n\n#########################################################################################################\n";
  echo -n "- User Confirmation required, please enter (Y to continue -OR N to stop/exit gracefully): "; read ConfY_N;
                
## DONT show the below echo on Screen STDOUT. Use >>.
  ConfY_N="$(echo ${ConfY_N} | tr a-z A-Z)";
  echo -e "\n# User Selected: \"${ConfY_N}\" ####################################################################################\n\n\n";
  copy
  if [[ "${ConfY_N}" == "N" ]];
  then
      echo -e "User requested to EXIT out gracefully.\n\n";
      #echo -e "`show_script_data`";);
      exit;
      fi
done
copy ()
{
mv ${IM_StartDt} ${IM_CloseDt}
}

# 2  
Old 04-26-2012
Code:
read ConfY_N

Please check the read happens on some file or tty. It has to be ideally done on tty

Code:
read a < /dev/tty

# 3  
Old 04-26-2012
Quote:
set_IM_CloseDt ()
{
There is no } to terminate this function. This causes a lot of the subsequent code to not execute.


Other quirks. The function copy is misplaced in the script. It needs to be well above the point where it is first called. Also, the function copy is called before the script actions the users request to exit!

This is unix script not Oracle. Do not terminate lines with semi-colon characters. Wherever you have a semi-colon as the last character in a line, remove that character. In some Shells it can give very strange behaviour.


Quote:
set_${var}
This does nothing. Maybe you meant set ${var} ?

Last edited by methyl; 04-26-2012 at 08:10 AM..
# 4  
Old 04-26-2012
You can also simplify the Yes/No loop
Code:
until false
do
  echo -e "\n\n\n###############################################################
##########################################\n";
  read -p "- User Confirmation required, please enter (Y to continue -OR N to st
op/exit gracefully): " ConfY_N;

  echo -e "\n# User Selected: \"${ConfY_N}\" ###################################
#################################################\n\n\n";
  # copy
  if [[ "${ConfY_N}" == "N" || $ConfY_N == 'n' ]]; then
      echo -e "User requested to EXIT out gracefully.\n\n";
      exit 0;
  fi
done

# 5  
Old 04-27-2012
This code is working now.I have corrected.This means set_${var}; if the variable doesn't have anything then function will prompt and ask the user for input.The function has wriiten this way either you can pass the arugment ,if not then it will prompt for user

Thanks to all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to shutdown Linux box with user confirmation?

Hi Guru's Am looking for linux reboot command which get executed after user confirmation .Can someone please help me with this.:confused::confused::confused: (6 Replies)
Discussion started by: kapil514
6 Replies

2. Shell Programming and Scripting

[Solved] For loop help

Hello, This is really breaking my head. I request you help to solve this problem. I have a list of files at the source directory (/tmp) as below, NewTransfer_20131202_APAC.dat NewTransferFile_20131202_APAC.dat NewTransfer_20131203_APAC.dat NewTransferFile_20131203_APAC.dat... (3 Replies)
Discussion started by: sravicha
3 Replies

3. Shell Programming and Scripting

[Solved] Problem with if-then-else loop

Hi, i have a problem with this script: for i in $(cat list_ip_switch) do if if ; then echo "found ip" else echo "not found ip" fi done cat list_ip_switch 10.155.249.171 10.155.249.172 (3 Replies)
Discussion started by: elilmal
3 Replies

4. Shell Programming and Scripting

[Solved] Issue with using for loop as for in {2..6} in korn shell

Hi i have to cut columns 2 to 6 from a file and assign it to arrays , The following code works for ctcol in 2 3 4 5 6; do set -A a$ctcol $(cut -d, -f $ctcol test_file) done how ever this does not work for ctcol in {2..6}; do set -A a$ctcol $(cut -d, -f $ctcol test_file)... (4 Replies)
Discussion started by: 100bees
4 Replies

5. Shell Programming and Scripting

[Solved] Simple script not working- for loop

Hi all, Please guide me writing this script Follwing is the file which I have created, which contains the files to be copied. cat system1-dr.txt /etc/passwd /etc/shadow /etc/group /etc/vfstab /etc/profile /etc/default/init /etc/shells /etc/dfs/dfstab /etc/dfs/sharetab... (11 Replies)
Discussion started by: manalisharmabe
11 Replies

6. Shell Programming and Scripting

[solved] Question for using variables outside a while loop

I want to get newvar outside the while any ideas? while read myline; do var=${myline} newvar1=$(let "$var") done echo $newvar1 I found it its ok now Thank you! (0 Replies)
Discussion started by: sanantonio7777
0 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Syntax error for awk in a loop

can some one please tell me what is the problem with my syntax:confused: I have 100 files in one folder 1. want to read each of the line by line 2. calculate their number of the words between the first word and the last word of each line 3. create file for each file with number of words... (8 Replies)
Discussion started by: A-V
8 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Simple while loop does not exit

spath="/home/user/k/${1}" dpath="/home/user/seq/Nov17/${1}" cd $dpath ls -1 $spath > list c=1 while read list newlist=`echo $list | sed 's/.gz//' ` newnewlist=`echo $newlist | sed 's/.fastq//' ` do echo $c echo $list c=$(($c+1)) (6 Replies)
Discussion started by: analyst
6 Replies

9. Shell Programming and Scripting

[Solved] Endless while loop when compare files

Hi All, I've written a script to read 2 files and compare the contents using while loop but somehow when $line is not found in test2, the script will continue looping. Below is my code, pls advise what could went wrong TIA Nick for line in test1.txt | while read line do grep -i... (4 Replies)
Discussion started by: Nick1971
4 Replies

10. Shell Programming and Scripting

[SOLVED] for loop to process files

I need to process a dirtree containing ms office files such that each file is stored as a variable and also, just the file file stem. Why? They will be using as input and output parameters for another script. For example /path/to/second_script -i filename.docx -o filename Here's what I... (1 Reply)
Discussion started by: graysky
1 Replies
Login or Register to Ask a Question