
Untitled
By: a guest on
Apr 28th, 2012 | syntax:
None | size: 1.18 KB | hits: 15 | expires: Never
concatenate inputs in bash script
./myBashScript.sh -flag1 exampleString1 exampleString2
function concatenate_args
{
string=""
for a in "$@" # Loop over arguments
do
if [[ "${a:0:1}" != "-" ]] # Ignore flags (first character is -)
then
if [[ "$string" != "" ]]
then
string+="_" # Delimeter
fi
string+="$a"
fi
done
echo "$string"
}
# Usage:
args="$(concatenate_args "$@")"
#!/bin/sh
firsttime=yes
for i in "$@"
do
test "$firsttime" && set -- && unset firsttime
test "${i%%-*}" && set -- "$@" "$i"
done
IFS=_ ; echo "$*"
#!/bin/sh
while ! test "${1%%-*}"
do
shift
done
IFS=_ ; echo "$*"
#!/bin/sh
shift
IFS=_ ; printf %s\n "$*"
echo $* | sed -e "s/ /_/g;s/[^_]*_//"
flag="$1"
shift
oldIFS="$IFS"
IFS="_"
the_rest="$*"
IFS="$oldIFS"
flag="$1"
shift
the_rest=""
pad=""
for arg in "$@"
do
the_rest="${the_rest}${pad}${arg}"
pad="_"
done
#!/bin/bash
paramCat () {
for s in "$@"
do
case $s in
-*)
;;
*)
echo -n _${s}
;;
esac
done
}
catted="$(paramCat "$@")"
echo ${catted/_/}