3

I'm trying to write a shell script that will make several targets into several different paths. I'll pass in a space-separated list of paths and a space-separated list of targets, and the script will make DESTDIR=$path $target for each pair of paths and targets. In Python, my script would look something like this:

for path, target in zip(paths, targets):
    exec_shell_command('make DESTDIR=' + path + ' ' + target)

However, this is my current shell script:

#! /bin/bash

packages=$1
targets=$2
target=

set_target_number () {
    number=$1
    counter=0
    for temp_target in $targets; do
        if [[ $counter -eq $number ]]; then
            target=$temp_target
        fi
        counter=`expr $counter + 1`
    done 
}

package_num=0
for package in $packages; do
    package_fs="debian/tmp/$package"
    set_target_number $package_num
    echo "mkdir -p $package_fs"
    echo "make DESTDIR=$package_fs $target"
    package_num=`expr $package_num + 1`
done

Is there a Unix tool equivalent to Python's zip function or an easier way to retrieve an element from a space-separated list by its index? Thanks.

3 Answers 3

4

Use an array:

#!/bin/bash
packages=($1)
targets=($2)
if (("${#packages[@]}" != "${#targets[@]}"))
then
    echo 'Number of packages and number of targets differ' >&2
    exit 1
fi
for index in "${!packages[@]}"
do
    package="${packages[$index]}"
    target="${targets[$index]}"
    package_fs="debian/tmp/$package"
    mkdir -p "$package_fs"
    make "DESTDIR=$package_fs" "$target"
done
Sign up to request clarification or add additional context in comments.

Comments

3

Here is the solution

paste -d ' ' paths targets | sed 's/^/make DESTDIR=/' | sh

paste is equivalent of zip in shell. sed is used to prepend the make command (using regex) and result is passed to sh to execute

1 Comment

In this usage both paths and targets need to be paths to files.
0

There's no way to do that in bash. You'll need to create two arrays from the input and then iterate through a counter using the values from each.

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.