!/bin/bash
declare -a myarray # http://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html , bash 4+ readarray myarray < file_pathname # includes the newlines readarray -t myarray < file_pathname # do not include newlines in the array items echo ${#myarray[@]} # should be the same number as wc -l file_pathname echo ${myarray[@]} # all array contents
posix shell /bin/sh doesn't really support arrays
almost!
i=0;
while read t; do
#TOKENS[$i]=$t
#i=expr $i + 1
#echo $t
done < apitokens.txt
echo $i
LAST='' SECOND='' while read t; do SECOND=$LAST LAST=$t done < apitokens.txt echo "$SECOND , $LAST"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!/bin/bash
2012-06-08 johnpfeiffer
read file into array and get values based on identifiers (split by = )
split by =
grab the first token on the left
is it the IDENTIFIER
if so, then the token on the right is the VALUE
function lineContains {
echo DEBUG "$#" parameters passed
if [ "$#" -ne 2 ]; then echo "line parameter missing" exit 0 else STRING=$1 KEYWORD=$2
OLDIFS=$IFS
IFS='='
IFS='=' read -a tokenArray <<< "$1"
temp=${tokenArray[0]}
if [ "$KEYWORD" == "$temp" ];
then
echo "${tokenArray[1]}"
fi
ALTERNATE UNSUCCESSFUL METHOD
for word in $STRING
do
if [ "$word" == "$KEYWORD" ];
then
echo "FOUND $word equals $KEYWORD"
fi
done
IFS=$OLDIFS
fi return $VALUE }
MAIN
IDOID="storagegateway.storageGatewayOid" IDKEY="storagegateway.storageGatewaySignedHash" IDTYPE="storagegateway.storageGatewayType" IDDOWNLOAD="storagegateway.downloadWeblinkPrefixURL" IDUPLOAD="storagegateway.downloadWeblinkPrefixURL"
CONVERT FILE INTO ARRAY
i=0; file=$1 while IFS= read -r LINE do ARRAY[$i]=$LINE ((i++)) done <"$file"
echo "processed $i lines into ${#ARRAY[@]} lines in the array"
SEE IF ARRAY CONTAINS THE VALUES REQUIRED
for (( k = 0 ; k < ${#ARRAY[@]} ; k++ )) do
RESULT=$(lineContains "${ARRAY[$k]}" "$IDOID") if [ "$RESULT" != "" ]; then echo "$RESULT" fi
RESULT=$(lineContains "${ARRAY[$k]}" "$IDUPLOAD") if [ "$RESULT" != "" ]; then echo "$RESULT" fi
done
exit 0