until loop and read


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting until loop and read
# 1  
Old 02-06-2010
until loop and read

Hi guys
what I wanna do is to create a script where can I input several times a file - assume with read function that will be inserted into a temp. file for further processing. When I press q I want that the loop will stop and continue in my script

I typed this but the options q is not working

Hi guys
what I wanna do is to create a script where can I input several times a file - assume with read function that will be inserted into a temp. file for further processing. When I press q I want that the loop will stop and continue in my script

I typed this but its absolutely working as I hoped

Code:
#!/usr/bin/ksh
touch input.txt
echo "Insert name of file: "
#read filename
while [ "$filename" != "q" ]
read filename
do
        echo $filename >> input.txt
done
exit

Ok this does sends every $filename to input.txt but seends also q, which should led to stop the loop. Can you give me a hint what is wrong and also is there way to omit blank lines.

Thank you in advance

Last edited by pludi; 02-06-2010 at 03:13 PM.. Reason: code tags, please...
# 2  
Old 02-06-2010
How about ...

Code:
INPUT=' '
while [ true ]
do
  read INPUT
  case $INPUT in
    "q") break ;;
    "" ) continue ;;
    *  ) echo $INPUT >> FILE ;;
  esac
done

# 3  
Old 02-06-2010
Omg, thats briliant men , txs a lot when I look at the script I understand most of it except the statement while [ true ], can you explain
# 4  
Old 02-06-2010
Sort of "continue execution until forcibly interrupted" Smilie
# 5  
Old 02-06-2010
is there a way to test in the loop if the file exists?
# 6  
Old 02-06-2010
E.g.:

Code:
     *)
       if [ -f $INPUT ]
       then
         echo $INPUT >> FILE
       else
         echo "Nice try ;-)"
       fi
     ;;

# 7  
Old 02-06-2010
Ok, just made the script working txs for your help and working, the way I like it
Can you give me a hit what can I optimize of make it more beautiful?

Code:
#!/usr/bin/ksh
DATE=`date +%Y%m%d`
echo "###############################################################################"
echo "#####     Copy files from target to source folder, and change permission  #####"
echo "###############################################################################"
echo ""
echo -n " Source dir: "
read source_dir
while [ ! -d $source_dir ]
do
        echo " The source dir doesnt exists, select a valid one "
        echo -n " Source dir: "
        read source_dir
done
echo ""
echo -n " Target dir: "
read tar_dir

while [ ! -d $tar_dir ]
do
        echo " The target dir doesnt exists, select a valid one "
        echo -n " Target dir: "
        read tar_dir
done
echo ""
echo -n " New owner: "
read owner
while [ `grep $owner /etc/passwd | cut -f1 -d: | wc -l` -eq 0 ]
do
        echo " Invalid user "
        echo -n " New owner: "
        read owner
done
echo ""
echo -n " New group: "
read group
while [ `grep $group /etc/group | cut -f1 -d: | wc -l` -eq 0 ]
do
        echo " Invalid group "
        echo -n " New group: "
        read owner
done
echo ""
echo -n " rwx permissions: "
read perm
echo " - Insert filenames  - "
INPUT=' '
while [ true ]
do
  read INPUT
  case $INPUT in
    "q") echo " Thank you for using " && break ;;
    "" ) continue ;;
    *  ) #$INPUT >> FILE
echo ""
echo " File - $INPUT - Copying from $source_dir to $tar_dir "
if [ -f ${source_dir}/${INPUT} ]
        then
                if [ -f ${tar_dir}/${INPUT} ];
                        then
                                mv ${tar_dir}/${INPUT} ${tar_dir}/${INPUT}_${DATE}
                                echo " File $INPUT backup --> ${INPUT}_${DATE} "
                fi
                cp ${source_dir}/${INPUT} ${tar_dir}/${INPUT}
                chown $owner:$group ${tar_dir}/${INPUT}; chmod $perm ${tar_dir}/${INPUT}
        else
                echo " File - $INPUT doenst exist "
fi     

echo " File - $INPUT - copied  - $owner:$group - $perm "
echo ""
;;
  esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While read line loop

Based on text file: PATH:/media/hdd/Media/Video/title1 FILE:/media/cache/281662-14.jpg PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg PATH:/media/hdd/Media/Video/title4 FILE:/media/cache/281662-17.jpg... (12 Replies)
Discussion started by: TiedCone
12 Replies

2. Shell Programming and Scripting

Help with while read loop

Hey all, Tried searching the forums but my search-fu may not be strong today; please feel free to redirect me if I have simply missed a post that would be helpful! Trying to create a while loop that reads a list of configuration files, checks for a line that starts with "SOME_CMD" and ends... (4 Replies)
Discussion started by: jdwyer
4 Replies

3. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

4. Shell Programming and Scripting

grep within while read loop

I have a key file $ cat klist 5 N:8855 CASA VERDE ROAD :32827 :ORLAND 5 N:585 MOLLY LANE :30189 :WOODST 5 N:320 NINA ROAD :32304 :TALLAH and a data file, see example of the line below: N:RT 15 & N 7TH STREET :17837 :U SAVE I need to search by key (2nd field) from klist... (6 Replies)
Discussion started by: migurus
6 Replies

5. Shell Programming and Scripting

Need help with for loop with while read

Hello, The code below pulls printer information from a three-column file (set by the ${MULTIFILE} variable at the end), that has the printer queue name, IP, and type of printer. It is supposed to use this info to add the printers in the list to one, or more servers . The input file... (3 Replies)
Discussion started by: LinuxRacr
3 Replies

6. UNIX for Dummies Questions & Answers

if elif loop with read

I am trying to setup an if, elif statement that is dependant on a variable. If the user does not enter a valid option I would like it to prompt them again and start the loop over. Here is what I have so far: echo -n "enter variable (a, b, or c): " read freq if ; then echo "a" elif ;... (2 Replies)
Discussion started by: BStanley346
2 Replies

7. Shell Programming and Scripting

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

8. Shell Programming and Scripting

Need Help : while read loop

hi all, Can you please help me with this issue ? while ####infinite loop do cat file1 |while read var1 var2 do func1 $var1 $var2 done cat file2 | while read var11 var22 do func2 $var11 $var22 done done func1 is called till all the values are read... (2 Replies)
Discussion started by: Navatha
2 Replies

9. Shell Programming and Scripting

Please explain read in a while loop

I have a script which tries to read input from a user for every value read from a file. The input file is #> more testfile TEST1 | D200 | 12345601 | | ABC company | m TEST2 | D201 | 12345602 | | ABC company | m The script test.sh is as follows while read line do read test?"Enter a... (5 Replies)
Discussion started by: jerardfjay
5 Replies

10. UNIX for Dummies Questions & Answers

read inside a while loop

Hi all, In a while loop, like below... while read line do read choice case $choice in 1) echo "xxx" esac done < file why I can't run the read choice???? (3 Replies)
Discussion started by: dta4316
3 Replies
Login or Register to Ask a Question