Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip function for shell scripts

Tags:

python

linux

bash

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.

like image 230
Evan Kroske Avatar asked May 11 '26 01:05

Evan Kroske


2 Answers

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
like image 170
Philipp Avatar answered May 13 '26 15:05

Philipp


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

like image 31
Taha Jahangir Avatar answered May 13 '26 13:05

Taha Jahangir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!