Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 1.18 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. concatenate inputs in bash script
  2. ./myBashScript.sh -flag1 exampleString1 exampleString2
  3.        
  4. function concatenate_args
  5. {
  6.     string=""
  7.     for a in "$@" # Loop over arguments
  8.     do
  9.         if [[ "${a:0:1}" != "-" ]] # Ignore flags (first character is -)
  10.         then
  11.             if [[ "$string" != "" ]]
  12.             then
  13.                 string+="_" # Delimeter
  14.             fi
  15.             string+="$a"
  16.         fi
  17.     done
  18.     echo "$string"
  19. }
  20.  
  21. # Usage:
  22. args="$(concatenate_args "$@")"
  23.        
  24. #!/bin/sh
  25.  
  26. firsttime=yes
  27. for i in "$@"
  28. do
  29.     test "$firsttime" && set -- && unset firsttime
  30.     test "${i%%-*}" && set -- "$@" "$i"
  31. done
  32.  
  33. IFS=_ ; echo "$*"
  34.        
  35. #!/bin/sh
  36.  
  37. while ! test "${1%%-*}"
  38. do
  39.     shift
  40. done
  41.  
  42. IFS=_ ; echo "$*"
  43.        
  44. #!/bin/sh
  45.  
  46. shift
  47. IFS=_ ; printf %s\n "$*"
  48.        
  49. echo $* | sed -e "s/ /_/g;s/[^_]*_//"
  50.        
  51. flag="$1"
  52. shift
  53. oldIFS="$IFS"
  54. IFS="_"
  55. the_rest="$*"
  56. IFS="$oldIFS"
  57.        
  58. flag="$1"
  59. shift
  60. the_rest=""
  61. pad=""
  62. for arg in "$@"
  63. do
  64.     the_rest="${the_rest}${pad}${arg}"
  65.     pad="_"
  66. done
  67.        
  68. #!/bin/bash
  69. paramCat () {
  70. for s in "$@"
  71. do
  72.     case $s in
  73.         -*)
  74.             ;;
  75.         *)
  76.             echo -n _${s}
  77.             ;;
  78.     esac
  79. done
  80. }
  81.  
  82. catted="$(paramCat "$@")"
  83. echo ${catted/_/}