metalx1000

BASH Array of Inputs to JSON

Jan 23rd, 2025 (edited)
392
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.94 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2025  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. list=("fname" "lname" "phone" "dob")
  20. args=""
  21. for f in ${list[@]}; do
  22.   read -p "${f}: " a
  23.   args+="--arg $f $a "
  24. done
  25.  
  26. jq -n $args \$ARGS.named
  27.  
Advertisement
Comments
  • SergeySamoylov
    52 days (edited)
    # Bash 0.33 KB | 0 0
    1. #!/usr/bin/env bash                                                                          
    2.  
    3. list=("fname" "lname" "phone" "dob")
    4. args=()  # an array for args will be safer
    5. for f in "${list[@]}"; do
    6.   read -p "${f}: " a
    7.   args+=(--arg "$f" "$a")
    8. done
    9. jq -n "${args[@]}" '$ARGS.named'  # no need for potentially dangerous 'eval' now
    10.  
Add Comment
Please, Sign In to add comment