Shell script prompt for first parameter ($1) then run

I have to run post configuration script like this script file is post_config.sh

post_config.sh 1234    #1234  is node ID

according to given node ID all script run
..
..
But i want to post_config.sh script should ask for node ID then run
like this..

post_config.sh             #i want to run this file only not with node ID
Enter the node ID here:    #asking for node id here i can put node id like 1234

Eg...
post_config.sh script is like this..

#This is script for Node Post configuration
#echo "Running script for $1"
 
echo "                      _                             "
echo "                      _                             "
echo "PKG6 Node Post Confg Start.............."
echo "                      _                             "
echo "                      _                             "
 
sleep 5
/home/Nodes/kat/bin/cmd_sys $1 ......
/home/Nodes/kat/bin/cmd_sys $1 ......
/home/Nodes/kat/bin/cmd_sys $1 ......
.
.

You can do something like this

NodeId=$1
if [ -z "$NodeId" ]; then
  printf "%s" "Enter the node ID here: "
  read NodeId
fi

and then use $NodeId instead of $1 everywhere

1 Like

Knowing what shell and operating system you're using could make a big difference here. In addition to what Scrutinizer suggested, maybe something like this will help with your problem:

#!/bin/ksh
case $# in
(0)	printf 'Enter node number to be processed: '
	read node;;
(1)	node="$1";;
(*)	printf 'Usage: %s [node]\n' "${0##*/}" >&2
	exit 1;;
esac
echo "Running script for node $node."
/home/Nodes/kat/bin/cmd_sys "$node"
... ... ...
1 Like
 [ROOT]post_config.sh
Enter node number to be processed: 2459
CONNECT FAIL.
CONNECT FAIL.
CONNECT FAIL.
CONNECT FAIL.

not working......
can any body help..

Could you post the modified script part that you ended up using....

Not without knowing how the script looks now.

printf 'Enter node number to be processed: '
read node
node="$node"

echo "Running script for node $node."
/home/Nodes/kat/bin/cmd_sys "$node"

This works
thanks team

This is unnecessary:

node="$node"

And since the error comes from:

/home/Nodes/kat/bin/cmd_sys "$node"

You would need to dig there.