Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >./jira -h
- ----------jira options----------
- Required:
- -d [data/issueID]
- -u [username] -> [username] is your JIRA username
- -p [password] -> [password] is your JIRA password
- Optional:
- -q -> quiet, i.e. no output to console
- -h -> help menu
- >./jira -u jsimmons
- ----------jira options----------
- Required:
- -d [data/issueID]
- -u [username] -> [username] is your JIRA username
- -p [password] -> [password] is your JIRA password
- Optional:
- -q -> quiet, i.e. no output to console
- -h -> help menu
- #!/bin/bash
- #using getopts to parse options
- while getopts ":hqd:u:p:" opt; do
- case $opt in
- h)
- help="true"
- ;;
- q)
- quiet="true"
- ;;
- d)
- data=$OPTARG
- ;;
- u)
- username=$OPTARG
- ;;
- p)
- password=$OPTARG
- ;;
- ?)
- echo "Invalid option: -$OPTARG" >&2
- ;;
- :)
- echo "Option -$OPTARG requires an argument." >&2
- ;;
- esac
- done
- #check if required options have been set
- if [[ -n $data && -n $username && -n $password ]]; then
- wget -q
- --http-user=$username
- --http-passwd=$password
- --header="Content-Type: application/json" [URI]
- #placing issue info into variable
- response=$(< $data)
- #using heredoc to run python script
- #python script uses regular expressions to find the value of the field
- #customfield_10701 ie the branch version
- output=$(python - <<EOF
- import re
- matchObj = re.search(
- r'(?<=customfield_10701":").*(?=","customfield_10702)',
- '$response',
- re.I
- )
- if(matchObj):
- print(matchObj.group())
- EOF
- )
- #writes branch version in .txt file
- echo $output>branchversion.txt
- #prints the branch version if the quiet option hasn't been set
- if [ -z $quiet ]; then
- echo "-------------------------------------------"
- echo ""
- echo "The branch version for issue $data is:"
- cat branchversion.txt
- echo ""
- fi
- #removes file that wget creates containing all data members for the issue
- rm $data
- elif [ -n $help ]; then
- #if help option has been set
- echo ""
- echo "----------jira options----------"
- echo "Required:"
- echo "-d [data/issueID]"
- echo "-u [username] -> [username] is your JIRA username"
- echo "-p [password] -> [password] is your JIRA password"
- echo ""
- echo "Optional:"
- echo "-q -> quiet, i.e. no output to console"
- echo "-h -> help menu"
- echo ""
- #http GET data members for issue
- else
- #if not all required options or help have been set
- echo "Error: Missing argument(s)"
- echo "Usage: ./jira [option(s)] -d [data] -u [username] -p [password]"
- echo ""
- echo "Try: ./jira -h for more options"
- fi
- if [ ... ]; then #posix compliant condition tests
- if [[ ... ]]; then #extended condition tests
- > if [ -n $unsetVar ];then echo yes ; fi
- yes
- >
- > if [ -n "$unsetVar" ];then echo yes ; fi
- >
- > if [[ -n $unsetVar ]];then echo yes ; fi
- >
- #!/bin/bash
- URI='...whatever...'
- usage() {
- # print optional error message and help message and exit.
- [ -z "$*" ] || printf "%snn" "$*"
- # or, if you don't want help printed with every error message:
- # [ -z "$*" ] || printf "%snnUse -h option for help.n" "$*" && exit 1
- cat >&2 <<__EOF__
- ----------jira options----------
- Required:
- -d [data/issueID]
- -u [username] -> [username] is your JIRA username
- -p [password] -> [password] is your JIRA password
- Optional:
- -q -> quiet, i.e. no output to console
- -h -> help menu
- __EOF__
- exit 1
- }
- quiet=''
- #using getopts to parse options
- while getopts ":hqd:u:p:" opt; do
- case $opt in
- h) usage ;;
- q) quiet='true' ;;
- d) data="$OPTARG" ;;
- u) username="$OPTARG" ;;
- p) password="$OPTARG" ;;
- ?) usage "Invalid option: -$OPTARG" ;;
- :) usage "Option -$OPTARG requires an argument." ;;
- esac
- done
- # check for required options
- [ -z "$data" ] && usage '-d is required'
- [ -z "$username" ] && usage '-u is required'
- [ -z "$password" ] && usage '-p is required'
- # your embedded python overkill could easily be done with `sed`
- # or `awk`. or maybe `jq` as the wget output is json.
- # but without a sample of the input I can't provide a guaranteed
- # working example. The awk script below is a first attempt at
- # extracting the value of `customfield_10701`
- wget -q --http-user="$username"
- --http-passwd="$password"
- --header='Content-Type: application/json'
- -O -
- "$URI" |
- awk -F': ' '/"customfield_10701"/ {
- gsub(/[",]/,"",$2);
- print $2
- }' > branchversion.txt
- # alternatively, if you have `jq` installed, try piping wget's
- # output through something like `jq -r .customfield_10701` instead of awk.
- #prints the branch version if the quiet option hasn't been set
- if [ -z "$quiet" ]; then
- echo "-------------------------------------------"
- echo ""
- echo "The branch version for issue $data is:"
- cat branchversion.txt
- echo ""
- fi
Add Comment
Please, Sign In to add comment