shell处理文件,进行字符串格式化,添加单引号或者双引号
运行示例:
159_linuxdba -->cat 3.sql
830
459
513
159_linuxdba -->gs 3.sql
830, 459, 513
159_linuxdba -->gs 3.sql 1
'830', '459', '513'
159_linuxdba -->gs 3.sql 2
"830", "459", "513"
shell代码:
#!/bin/bash
if [ $# == 0 ];then
echo "没有带文件";
exit 0;
fi
>.tempfile
function for_in_file_two(){
for i in `cat $1`
do
echo '"'$i'",'>>.tempfile
done
}
function for_in_file_one(){
for i in `cat $1`
do
echo "'"$i"',">>.tempfile
done
}
function for_in_file_zero(){
for i in `cat $1`
do
echo $i','>>.tempfile
done
}
if [[ $2 = '2' ]];
then
for_in_file_two $1
elif [[ $2 = '1' ]];
then
for_in_file_one $1
else
for_in_file_zero $1
fi
echo ''
b=`cat .tempfile`
a=${b%\,*}
echo $a
echo ''
rm .tempfile