Imran Afzal
Imran Afzal

Reputation: 11

How do I rerun a script?

I have a script that will check if a record exist and if it does then it will exit. Now I am trying to find out how to re-run the script if record exist:

#!/bin/bash

echo Please enter hostname?
read hostname
echo
grep -q $hostname /home/user/ps/database
if [ $? -eq 0 ]
then
  echo Record exist
  echo
fi

Now I want to re-run the script if record exist. Please help

Upvotes: 1

Views: 2138

Answers (1)

chepner
chepner

Reputation: 531205

You just need a simple loop:

#!/bin/bash

while :; do
  echo Please enter hostname?
  read hostname
  echo

  if grep -q $hostname /home/user/ps/database
  then
    echo Record exist
    echo
  else
    break
  fi
done

Upvotes: 2

Related Questions