简体   繁体   中英

How to pass array of arguments in shell script?

Right now ia have a working script to pass 2 arguments to a shell script. The script basically takes a ticket# and svn URL as arguments on command line and gives an output of all the revisions that have been changed associated with that ticket# (in svn comments).

#!/bin/sh

jira_ticket=$1
src_url=$2


revs=(`svn log $2 --stop-on-copy | grep -B 2 $1 | grep "^r" | cut -d"r" -f2 | cut -d" " -f1| sort`)

for revisions in ${!revs[*]}
    do
    printf "%s %s\n" ${revs[$revisions]}
done

Output:

4738
4739
4743
4744
4745

I need some help to pass an array of arguments - meaning more than one ticket# and give the output of revisions associated with those ticket numbers that get passed as args to the script.

I don't think POSIX shell has arrays, so be plain and use #!/bin/bash

I would put the url as the first arg, and all the reset are tickets

#!/bin/bash
revs=()
src_url=$1
svn_log=$(svn log "$src_url" --stop-on-copy)
shift
for jira_ticket in "$@"; do   
    revs+=( $(grep -B 2 "$jira_ticket" <<< "$svn_log" | grep "^r" | cut -d"r" -f2 | cut -d" " -f1) )
done
for revisions in $( printf "%s\n" "${!revs[@]}" | sort )
    do
    printf "%s %s\n" ${revs[$revisions]}
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM