0

I'm trying to create a script, where multiple user inputs are being accepted. When I call the script, and provide multiple inputs the script won't execute when multiple inputs are provided. What am I doing wrong here.

#! /bin/bash

server_list=()

echo "Enter server name:  "
readarray -t  servername

declare -p servername

sudo showsudolocal $servername | tr -d '$' | sed -e '/^$/d' | tee output.txt
sed -i 1,8d output.txt

mapfile -t myArray < output.txt
count=${#myArray[@]}


for (( i=1; i<$count; ))
#for i in `seq 1 $count`
do
        str="${myArray[$i]}"
        echo "str is $str"

        IFS=',' read -r -a array <<< "$str"
        i=$((i+2))
        username="${array[0]}"
        groupname="${array[1]}"


        echo "Username is $username"
        if [[ -z "${array[0]}" ]] || [[ "${array[0]}" == *'*'* ]]
        then
                echo "group"
                sudo docentcmd $servername centclicmd adquery group $groupname
                cat /etc/group | grep $groupname
        else
                echo "User"
                sudo docentcmd $servername centclicmd adquery user $username
                cat /etc/passwd | grep $username
        fi

done
BB956
  • 31
  • 3
  • 11
  • 1
    What exactly do you mean by multiple inputs? And what does it mean that it doesn't get executed? Are you getting some error messages or something? Perhaps you mean that shell is waiting for you to press `ctrl-d` which is a normal behavior if you use `readarray` like this. Please elaborate. – PesaThe Jul 16 '18 at 16:51
  • @PesaThe As the user provides 1 or more serve names, that are stored to variable $servername. When I try to execute after providing the server names, only the first line provided works and any other input is executing – BB956 Jul 16 '18 at 16:57
  • You have to send `EOF` with `ctrl-d`. `readarray` doesn't just magically know when you've provided all server names you wanted :) And the server names will be then stored in an **array**. – PesaThe Jul 16 '18 at 17:00
  • @PesaThe Thank you, it's finally running now. Except any other values provided after the first input isn't being executed. – BB956 Jul 16 '18 at 17:03
  • 1
    Because `$servername` is an **array**. You can just loop over all the server names stored in it. See: [Loop through an array of strings in Bash?](https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash) – PesaThe Jul 16 '18 at 17:04
  • Why are you using arrays at all? This would be easier to use if you had one line of input with 'servername username groupname' on each line and just did `while read server user name; do ...` – William Pursell Jul 16 '18 at 20:34

0 Answers0