Skip to content

Instantly share code, notes, and snippets.

@ollyblue
Created October 9, 2015 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ollyblue/d0d12d628ab39a4b3762 to your computer and use it in GitHub Desktop.
Save ollyblue/d0d12d628ab39a4b3762 to your computer and use it in GitHub Desktop.
shell code
root@localhost# cat data
- Name:"AAA"
- Age:"20"
- Name:"BBB"
- Age:"30"
root@localhost#cat p.sh
#!/usr/bin/env bash
IFS=":"
declare -i line=0
declare -i index=0
declare -a arr_name
declare -a arr_age
while read item_name item_value
do
v=$((line%2))
case $v in
0)
arr_name[$index]=${item_value}
;;
1)
arr_age[$index]=${item_value}
;;
esac
let "index = index + v"
let "line = line + 1"
done < data
for((i=0; i < $index; ++i))
do
if [[ ${arr_name[${i}]} == "\"AAA\"" || ${arr_name[${i}]} == "\"BBB\"" ]];then
echo "- Name:${arr_name[${i}]}"
echo "- Age:${arr_age[${i}]}"
echo "- Gender:\"F\""
fi
done
@Artoria2e5
Copy link

数字下标的话不需要 "${arr[$i]}",用 "${arr[i]}" 就是。另外 (( 里面也一样,不涉及复杂的替换仅限于数组和变量的话用不上 $

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment