0

I am trying to create my first bash script:

echo "What file are we looking for: "
read FILE

while [ 1 ]
do 
    ls -lah | grep $FILE
    sleep 1;
    clear
    
    # Some way to detect user input
    if [ user-input ]
       then
        echo "Input found"
        exit 1;

    fi

done

Is there a way to look for user input without pausing the program? When I used read input before the if statement, the program stopped until input was...inputted.... The program is supposed to continuously output the file I am look using ls and clear so I can monitor the size as it grows, but when the user inputs any key stroke the program exits.

Like I said this is my first bash script, I do know python pretty well and understand 'coding' but not bash.

Thanks

1 Answer 1

1

check Bash input without pausing the script?, seems like a duplicate. from that link:

read -t0 can be used to probe for input if your process is structured as a loop

 #!/bin/bash

 a='\|/-'
 spin()
 {
  sleep 0.3
  a="${a:1}${a:0:1}"
  echo -n $'\e'7$'\r'"${a:1:1}"$'\e'8
 }

 echo 'try these /|\- , dbpq , |)>)|(<( , =>-<'

 echo -n "  enter a pattern to spin:"
 while true
 do
   spin
   if read -t0 
   then
     read a
     echo -n "  using $a enter a new pattern:" 
   fi
 done

else you could run one command in the background while promptiong for input in the foreground. etc...

Sign up to request clarification or add additional context in comments.

2 Comments

this worked and yes it does seem to be a replicate question! Now I need to find out how to cancel the imputed text because the shell thinks it is a command when the program exits! Thanks
Not sure what do you mean.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.