0

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
3
  • 2
    Please clarify first of all which shell you are using. You have tagged your posting as shell, which suggests Posix-Shell, and your #! line also points towards Posix-Shell, but you are creating an array, and this would not work in Posix-Shell, but in bash, Zsh or ksh. Commented Mar 19, 2018 at 9:59
  • @user1934428 actually, it's bash. I'm using "git bash" on Windows. Commented Mar 19, 2018 at 11:35
  • Just use maven_params=() unconditionally; if $# is 0, then maven_params=(), which is even better than maven_params="", since "${maven_params[@]}" will then simply disappear, rather than expand to the empty string and be passed to mvn as a distinct argument. Run your code through shellcheck.net; you have a lot of small things to fix. Commented Mar 19, 2018 at 19:17

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh, if I have a few elements in my array then printf will be applied two times in a row... Actually, how to avoid this behavior?
I found a solution. I had to change in print $@ on $*.
With "$*" you make it one argument, while "$@" makes it separate arguments. How often printf uses the format, depends of course not only on the number of arguments, but also on the number of %s you have in the format.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.