We help IT Professionals succeed at work.

A simple shell script

clo1
clo1 asked
on
I usually has to manually repeat the command,

netstat | grep https

for many times to see if there are any user using the web server, I would like to know how to write a shell script to repeat this task automatically and then do something once it has greped nothing. Thanks
Comment
Watch Question

Peter KwanAnalyst Programmer

Commented:
Assuming you are using sh,

while [ `netstat | grep https`"x" = "x" ]
do
# do something else
done
CERTIFIED EXPERT

Commented:
if [ `netstat|grep https` ]; then
    # do somthing
fi

Author

Commented:
I would like to have a complete format of script. Thanks
Peter KwanAnalyst Programmer

Commented:
#!/bin/ksh        # check the path of ksh and change it if necessary

result=`netstat | grep https`
while [ "x"$result = "x" ]
do
  # the result is empty
  # do something else
  result=`netstat | grep https`
done
Peter KwanAnalyst Programmer

Commented:
Sorry, the last one is incorrect.

#!/bin/ksh        # check the path of ksh and change it if necessary

result=`netstat | grep https`
while [ "x"$result != "x" ]
do
 # the result is not empty
 result=`netstat | grep https`
done

# Here, the result is empty
# do something

Author

Commented:
I have edited the script as follow:

#!/bin/sh

result=`netstat | grep https`
while [ "x"$result != "x" ]
do
    result=`netstat | grep https`
done

echo 'HELLO'

But I got this error when I try to run the script.
./chk.sh: [xtcp: command not found

Please help. Thanks a lot !!
Analyst Programmer
Commented:
You can replace "x"$result with "x$result", and that should solve the problem.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.