Skip to main content
Remove unnecessary export, trailing semi-colons, and trim spaces
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

A script - named script.sh in the example below - containing the following code should get you the output you described:

#!/bin/bash

#USER INPUT
read -p "Specify the package: " -a vals

#COUNTER
export i=1;i=1

#ARRAY ELEMENT LOOP
for v in "${vals[@]}"; do
  
   #Printing output  
   echo -n "Pkg$i=$v";       "Pkg$i=$v"

   #Increment counter and add the comma, or break out of loop if $i==
   i=$((i+1)); 
   if [[ $i -le ${#vals[@]} ]] ;]]; then 
        echo -n ",";            "
   else 
        break 
   fi 

done

To run the script from the shell, you need to first make it executable (chmod) and then execute it:

chmod +x script.sh
./script.sh

Its input/output will look like this:

Specify the package: perl runtime pool tools
Pkg1=perl,Pkg2=runtime,Pkg3=pool,Pkg4=tools

A script - named script.sh in the example below - containing the following code should get you the output you described:

#!/bin/bash

#USER INPUT
read -p "Specify the package: " -a vals

#COUNTER
export i=1;

#ARRAY ELEMENT LOOP
for v in "${vals[@]}"; do
  
   #Printing output  
   echo -n "Pkg$i=$v";       

   #Increment counter and add the comma, or break out of loop if $i==
   i=$((i+1)); 
   if [[ $i -le ${#vals[@]} ]] ; then 
        echo -n ",";            
   else 
        break 
   fi 

done

To run the script from the shell, you need to first make it executable (chmod) and then execute it:

chmod +x script.sh
./script.sh

Its input/output will look like this:

Specify the package: perl runtime pool tools
Pkg1=perl,Pkg2=runtime,Pkg3=pool,Pkg4=tools

A script - named script.sh in the example below - containing the following code should get you the output you described:

#!/bin/bash

#USER INPUT
read -p "Specify the package: " -a vals

#COUNTER
i=1

#ARRAY ELEMENT LOOP
for v in "${vals[@]}"; do

   #Printing output  
   echo -n "Pkg$i=$v"

   #Increment counter and add the comma, or break out of loop if $i==
   i=$((i+1))
   if [[ $i -le ${#vals[@]} ]]; then
        echo -n ","
   else
        break
   fi

done

To run the script from the shell, you need to first make it executable (chmod) and then execute it:

chmod +x script.sh
./script.sh

Its input/output will look like this:

Specify the package: perl runtime pool tools
Pkg1=perl,Pkg2=runtime,Pkg3=pool,Pkg4=tools
Source Link
NetIceCat
  • 2.4k
  • 1
  • 16
  • 28

A script - named script.sh in the example below - containing the following code should get you the output you described:

#!/bin/bash

#USER INPUT
read -p "Specify the package: " -a vals

#COUNTER
export i=1;

#ARRAY ELEMENT LOOP
for v in "${vals[@]}"; do
  
   #Printing output  
   echo -n "Pkg$i=$v";       

   #Increment counter and add the comma, or break out of loop if $i==
   i=$((i+1)); 
   if [[ $i -le ${#vals[@]} ]] ; then 
        echo -n ",";            
   else 
        break 
   fi 

done

To run the script from the shell, you need to first make it executable (chmod) and then execute it:

chmod +x script.sh
./script.sh

Its input/output will look like this:

Specify the package: perl runtime pool tools
Pkg1=perl,Pkg2=runtime,Pkg3=pool,Pkg4=tools