I have written a little script. When it is finished, it will set the environment variables I need and start a Node server for me. I made the script so that I have a command line interface to change the environment variables on startup without having to remember.
This is what I have so far...
if [ $1 = "d" ] || [ $1 = "default" ]
then
export PORT=80
export ENV=development
export ENDPOINT=development
elif [ $# -eq 0 ]
then
printf "\n\n-------------------\n\nPort Options:\n[1] 80\n[2] 3000\n[3] Custom\n\n-------------------\n\n"
read -p "Select Option: " PORT_OPTION
if [ "$PORT_OPTION" -eq 1 ]
then
REQUESTED_PORT=80
elif [ "$PORT_OPTION" -eq 2 ]
then
REQUESTED_PORT=3000
elif [ "$PORT_OPTION" -eq 3 ]
then
read -p "Enter Requested Port Number: " REQUESTED_PORT
else
echo "Invalid slection...exiting."
exit
fi
if [ "$REQUESTED_PORT" -eq "$REQUESTED_PORT" 2> /dev/null ]
then
if [ "$REQUESTED_PORT" -lt 1024 ] && [ "$(id -u)" != "0" ]
then
echo "Must have Root authoritzation to use this port...exiting."
exit
else
if netstat -an | grep "$REQUESTED_PORT" > /dev/null
then
SERVICE_PIDS_STRING=`lsof -i tcp:$REQUESTED_PORT -t`
OLD_IFS="$IFS"
IFS='
'
SERVICE_PIDS=($SERVICE_PIDS_STRING)
IFS="$OLD_IFS"
printf 'Port is in use by the following service(s)...\n\n-------------------\n\nProcess : PID\n'
for PID in "${SERVICE_PIDS[@]}"
do
PROCESS_NAME=`ps -p $PID -o comm=`
printf "$PROCESS_NAME : $PID\n"
done
printf "\n-------------------\n\nPlease kill the procceses utilizing port $REQUESTED_PORT and run this script again...exiting."
exit
else
echo "NOTHING USING PORT"
fi
fi
else
echo "Invalid port number...exiting."
exit
fi
else
echo "Invalid argument...exiting."
exit
fi
I have searched around for a while trying to find a solution to my problem. previously line #37 was changed from read -a SERVICE_PIDS <<< "${SERVICE_PIDS_STRING}"
. Everything works well, and both versions of line #37 have worked for me...unless I'm running the script as sudo.
This wouldn't be a problem, but the system protects certain ports, so sometimes I need to use sudo on the script.
I'm receiving the following error when I run my script with sudo...
./Start.sh: 36: ./Start.sh: Syntax error: "(" unexpected (expecting "fi")
Why does using sudo break my script, and how can I remedy this issue?
P.S. I would prefer to get this working with Shell Script rather than Bash. (I may be saying the wrong thing here, but my understanding is that if I use pure Shell Script the script can be run from any UNIX system, and my partner uses a Mac.)