Having an input for a variable, dynamically create values for another variable using a static list
I have a set of hostnames, I would want to assign these hostnames to the variable SERVERS or CLIENTS. When the hostname from the given list is selected for SERVERS it should get removed from the CLIENTS variable.
Note:- variable SERVER might have more than one hostname
LIST_OF_NODEScn01,cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10INPUT=cn01
OUTPUT:-
SERVERS=cn01 CLIENTS=cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10 (echo "server $SERVERS"; echo "client $CLIENTS") > cn01.cmdINPUT=cn02
OUTPUT:-
SERVERS=cn02 CLIENTS=cn01,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10 (echo "server $SERVERS"; echo "client $CLIENTS") > cn02.cmd SERVERS=cn01,cn02 CLIENTS=cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10 (echo "server $SERVERS"; echo "client $CLIENTS") > cn01_cn02.cmd
INPUT:- Values for the SERVERS for example
If cn03, cn07 is provided as input then
SERVERS=cn03,cn07 CLIENTS=cn01,cn02,cn04,cn05,cn06,cn08,cn09,cn10 (echo "server $SERVERS"; echo "client $CLIENTS") > cn03_cn07.cmd
I tried the following
for i in cn{01..10}; do
echo $i
sed "s/$i//g" nodes.txt | sed 's/,,/,/g'| sed 's/,*$//g' | sed 's/^,//g'
done
If I understood your requirement correctly, could you please try following.
cat script.ksh
echo "Enter cn values in form of cn01 or cn02 etc here...."
read value
SERVERS="$value"
LIST_OF_NODES="cn01,cn02,cn03,cn04,cn05,cn06,cn07,cn08,cn09,cn10"
awk -v servers="$SERVERS" -v clients="$LIST_OF_NODES" '
BEGIN{
s1=","
split(servers,array,",")
for(i in array){
gsub(s1 array[i] s1,s1,clients)
gsub("^"array[i] s1,"",clients)
gsub(s1 array[i]"$","",clients)
}
gsub(/,/,"_",servers)
print clients > servers".cmd"
}'
Created above script. Now following is when we run the script:
./file.ksh
Enter cn values in form of cn01 or cn02 etc here....
cn04,cn07
Output file named cn04_cn07.cmd will be created.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With