Simple loop query


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simple loop query
# 1  
Old 07-17-2007
Simple loop query

Hi All

Just started with shell scripts and am stumped by, what is to most of you no doubt, a simple issue.
All I'm trying to do is prompt a user for input and writing to a log file. If the user types the word 'stop', then the program should halt. If the word typed is 'clear', then the log file should be cleared.

Starting simply(!), all I've got so far is:

LOGDIR=/some/dir/
TARGETFILE="whatever"

ans="abc"

while [ ${ans} != "stop" }
do
echo "Enter text:"
read ${ans}
echo ${ans} >>${LOGDIR}/${TARGETFILE}
done

All that happens is the original value of "abc" is written to the log. Obviously the variable isn't being overwritten by the user input. Can anyone help with basic help for a newbie?
# 2  
Old 07-17-2007
Code:
while true
do
  echo "Enter text:"
  read ans
  case ${ans} in
    "clear") : > ${TARGETFILE};;
    "stop")  break;;
    *)        echo ${ans} >>${TARGETFILE};;
  esac
done

# 3  
Old 07-17-2007
Works great!

Agradecimentos muito muito, Shell Life
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If loop query

Hi, its getting aborted with below IF loop, can plz guide me what im missing here if || ; then echo "print $APPEND" fi (3 Replies)
Discussion started by: JSKOBS
3 Replies

2. Programming

Oracle simple SQL query result in: ORA-08103: object no longer exists

Dear community, please help with a query on Oracle. I'm using SQLPlus (but with SQLDeveloper is the same) to accamplish a sinple query like: select count(*) from ARCHIT_D_TB where (TYP_ID=22 OR TYP_ID=23) and SUB_TM like '%SEP%' and CONS=1234This is a very simple query that works perfect until... (5 Replies)
Discussion started by: Lord Spectre
5 Replies

3. Shell Programming and Scripting

For loop query...

Hi all... I am using a for loop for another part of AudioScope... Consider this code:- for n in {0..100} do if }" = "another_string" ] then break fi done This works perfectly except I am not sure if breaking out of a 'for' loop might cause... (2 Replies)
Discussion started by: wisecracker
2 Replies

4. Shell Programming and Scripting

Infinite loop query

I have a script script.shwhich is scheduled to run at 11 AM everyday. # script.sh Code: ./scb_script.sh & unfortunately scb_script.sh is running today in infinite loop as respective files are not available. My question, when script.sh starts running tomorrow, will the old process be... (1 Reply)
Discussion started by: JSKOBS
1 Replies

5. Shell Programming and Scripting

Query regarding the if loop

Hi friends, One of my shell program(test.ksh) contains the following if loop.Can anyone please explain me how this loop will work The shell script will be executed as test.ksh -d all The value of variables are user=all opt=-d if && then && _del_all_w=1 || _group="${1}"... (1 Reply)
Discussion started by: kavithakuttyk
1 Replies

6. Programming

A simple C program query ...

Given the following code inside the function ext3_write_super(): (It's there in Linux kernel 2.6.27.59) static void ext3_write_super (struct super_block * sb) { if (mutex_trylock(&sb->s_lock) != 0) BUG(); sb->s_dirt = 0; } The conditional test at if... (2 Replies)
Discussion started by: Praveen_218
2 Replies

7. Shell Programming and Scripting

Query regarding for loop.

Filename.txt My First Line My Second Line ::::While Loop::: Program: while read line do echo "$line" done < Filename.txt output: My First Line My Second Line Is it possible to use for loop to get the same output. I have tried executing below code but i get every word of my file... (8 Replies)
Discussion started by: pinga123
8 Replies

8. Shell Programming and Scripting

Need Help !!! simple query

Dear, I have a alarm text file, containing minor and major alarms, i am intrested in Mojor alarm with its alarm header and next four lines in seperate file.... Can anybody help me with this below is the alarm file output. :SEV="MAJOR": Object-Instance % unit-type % bts nbr % 25 ... (5 Replies)
Discussion started by: Danish Shakil
5 Replies

9. Shell Programming and Scripting

A simple query on unix shell script

I want to write a script to go to particular path in file and run shell script from there. what will be shell script for the same. (2 Replies)
Discussion started by: shekhar_ssm
2 Replies

10. UNIX for Dummies Questions & Answers

simple sed query

hi, i would like to replace a string in a series of files with another string, without outputting to new files. is this possible? i've tried using sed, and started by trying to alter the contents of one file... sed 's/string1/string2/g' file.txt but while this does the replacement on... (2 Replies)
Discussion started by: schmark
2 Replies
Login or Register to Ask a Question