[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: disable IPv6 debian



On Sat, Apr 16, 2022 at 08:20:40AM +0800, wilson wrote:
> Can you help check if my this script has any issue?

> #!/bin/bash
> 
> PORT=$1
> if [ -z $PORT ];then

"$PORT" should be quoted here.
 
>   echo "$0 port"

As a usage message, this is rather minimal.  At least put "usage: " in
front of it.  Ideally it should also be written to stderr, not stdout.

echo "usage: $0 port" >&2

>   exit

And you should exit with a nonzero status here, to indicate that an error
occurred.

exit 1

> fi
> 
> PS=`lsof -i :$PORT |grep LISTEN |awk '{print $2}'`

Backticks are deprecated.  $( ) is preferred for command substitution.

"$PORT" should be quoted here as well.

grep LISTEN | awk '{print $2}' can be combined into a single command:

awk '/LISTEN/ {print $2}'

> 
> if [ -z $PS ];then

"$PS" should be quoted.

>   echo "no this port, or I don't have privilege to list the port"

Use >&2 to send the error message to stderr.

>   exit

exit 1

> fi
> 
> ps -efw |grep $PS |grep -v grep

"$PS" should be quoted.

You're also going to exit your script with the exit status from that
last grep command.  That's probably not what you want.  If it's not,
then an explicit "exit 0" at the end might be a good idea.

Or, as another choice, you might want to exit with the exit status of
the *first* grep.  In that case, switching them around would be better:

ps -efw | grep -v grep | grep "$PS"

But it depends on whether you actually want that behavior.

> 
> Thanks

As a final note, using ALL-CAPS VARIABLE NAMES is a bad practice.  All-caps
variable names are reserved for environment variables, or special shell
internal variables like PATH, BASH_VERSION, RANDOM and so on.  Your
ordinary variables should contain at least one lower-case letter, to avoid
a name collision.

I don't think PORT or PS are used by anything... yet... but eventually
you're going to shoot yourself in the foot by accidentally picking
something like USER or PATH which *is* in use.  So, it's best to break
the bad habits now.


Reply to: