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

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

How about ...

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

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

Sort of "continue execution until forcibly interrupted" :wink:

is there a way to test in the loop if the file exists?

E.g.:

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

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?

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