Evgeny Ignatik
Evgeny Ignatik

Reputation: 97

Duplicate of string in shell script

I faced a little trouble. I just wrote a simple shell script to show a few dependencies of maven in files. It works fine. But the only problem. During the process, I show an information about this process progress and so on (I described that in the example below). And one of the lines I display using printf is duplicating and it looks like:

Writing dependencies in ../path/all_dependencies.txt

-- Applying with "mvn dependency:tree -Dverbose ".....

-- Applying with "mvn dependency:tree -Dverbose -Dincludes=javax.servlet".....

I can't get why an additional line appears here. I revealed a lot of sources on the Internet but found nothing in which direction I have to dig. I guess it can be influenced by progress bar but can't understand how exactly.

And what is also interesting, why I get different lines? Maybe it's a feature how arrays work?...

I would appreciate any help/explanation/etc.

This is my script, I added comment before troubled line:

#!/bin/sh
start=$(date)
printf "\n Show dependencies from all projects \n $start"
printf "\n -----------------------------------"

#Paths to catalogs
PATH1=..some/path1
PATH2=..some/path2
PATH3=..some/path3

green='\033[0;32m'
red='\033[0;31'
nc='\033[0m'

#  mvn_params represents Maven Parametrs options
#  you can specify here all params you want to use when dependency tree will be applied
#
# E.G: -Dverbose -Dincludes=javax.servlet
#
if [ "$#" -eq 0 ]; then
    maven_params=""
else
    maven_params=( "$@" )
fi

array=(
    $PATH1
    $PATH2
    $PATH3
    )

cp /dev/null all-dependencies.txt

for element in ${array[@]}; do
    module=$element
    if [ -d "$module" ]; then
        cd $element
        full_path="dependencies.txt"
        printf "\n Writing dependencies in $module/$full_path"
        #  A duplicated line is below           
        printf "\n  -- Applying with \"mvn dependency:tree %s \"....." "${maven_params[@]}"

        sp='/-\|'
        printf ' '
        mvn dependency:tree "${maven_params[@]}" > $full_path &
        while [[ -n $(jobs -r) ]]; do
            printf '\b%.1s' "$sp"
            sp=${sp#?}${sp%???}
        done

        status_maven=$?
        cat $full_path >> ../all-dependencies.txt 
        if [ $status_maven -eq 0 ]; then
            printf "\b%.1s ${green}\\u2714${nc} Done\n"
        else
            printf "\b%.1s ${red}\\u274C${nc} Failed\n"
        fi
    else
        printf "\n ${red}\\u274C Failed. ${nc} $module: No such file or directory\n"
    fi
    
done
printf "\n ${green}DONE:${nc} File with all dependencies has been created: all-dependencies.txt"
exit 0

Upvotes: 0

Views: 92

Answers (2)

Evgeny Ignatik
Evgeny Ignatik

Reputation: 97

Thanks to user1934428 I was able to think about array elements representation in a shell. I found a simple solution. I hope it can be helpful for somebody who will also be looking for an answer.

SOLUTION:

In my case, I wrote "${maven_params[@]}" where [@] represents all arguments separated from each other. I had to use [*] instead. It provides using array elements in a row like $1$2..etc.

"$@" expands each element as a separate argument, while "$*" expands to the args merged into one argument

Upvotes: 1

user1934428
user1934428

Reputation: 22366

The behaviour you see, can be demonstrated by this example:

printf "\nParameter: %s\n" a b c

If you have only one formatting code (here: %s), but pass several arguments to printf, the string will be used over and over again, so the output of this statement is:

Parameter: a

Parameter: b

Parameter: c

In your case, it means that the array maven_params contains two elements.

Upvotes: 3

Related Questions