Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop script till user input is empty?

I am trying to make my script to repeat till the user leaves the block question empty. I just got the loop to run, but I can not find a way to make it possible to stop it when block is empty.

I hope some one can help me!!

#!/bin/tcsh -f
#

set word="start"
until ($word !=""); do

#First ask for Compound and Block Name.
echo -n "please enter block name: "
read block
echo -n "please enter compound name: "
read compound

#Now coping template with new name
#
cp Template $block
#
        for line  in `cat $block`;do
        echo $line | sed -e "s/test1/${block}/g" -e "s/test2/${compound}/g" >>./tmp124.txt
done

mv ./tmp124.txt $block

done
like image 787
Ken Avatar asked Dec 04 '25 10:12

Ken


1 Answers

Do you want to use bash or csh? You are using bash syntax but tagged your question csh and call tcsh in the first line of your code.

To answer your question, here are examples of how to iterate on standard input until some input is empty:

For tcsh:

#!/bin/tcsh

while ( 1 )
    set word = "$<"
    if ( "$word" == "" ) then
        break
    endif

    # rest of code...
end

For bash:

#!/bin/bash

while read word; do
    if [ -z $word ]; then
        break
    fi

    # rest of code...
done
like image 85
twalbaum Avatar answered Dec 07 '25 12:12

twalbaum