Shell如何传递字符串

Shell 在写函数的时候,有时候需要传递字符串,由于字符串中有空格,所以结果总是不对,下面写个小例子,解决这个问题:

#!/bin/bash

# value init
TT="adb shell ls -l /data/local/tmp/";
FINAL="";
loop_count=2;

# LoopEx: excute a programm in a loop, $1 is program,$2 is loop count,
function LoopEx()
{
	Ex=$1;
	local loop_count=$2;
	echo "Programm is $Ex, loop_count ==$loop_count"
	for i in $(seq $loop_count); do
		$Ex;
	done;
}

LoopEx $TT $loop_count

  这个输出的结果是:

Programm is adb, loop_count ==shell
seq: invalid floating point argument: shell

因为传递的字符串中有空格,解决这个问题如下,在调用函数传递参数的时候使用:

LoopEx "$TT" $loop_count 

得到正确的显示结果如下:

Programm is adb shell ls -l /data/local/tmp/, loop_count ==2
drwxrwxrwx root root 2010-01-01 23:10 CPlusPlus
-rw-rw-rw- root root 2 2014-08-07 01:54 PPAMonitorPort.pt
-rwxrwxrwx root root 153916 2014-04-17 08:33 libGLES_mali1.so
-rwxrwxrwx root root 10304 2014-04-17 08:42 libGLESv2Agent.so
-rwxrwxrwx root root 890744 2014-04-17 08:33 libppa.so
-rwxrwxrwx root root 259484 2014-04-17 08:34 librsagent.so
-rwxrwxrwx root root 471328 2014-04-17 08:33 libtunnel.so
drwxrwxr-x root root 2010-01-01 00:11 vp9Input
-rw-rw-rw- root root 24285 2014-08-07 02:30 vp9_inter_pred_4x4.cl
-rw-rw-rw- root root 40239 2010-01-01 23:56 vp9kernel.bin
-rwxrwxrwx root root 510012 2014-08-07 02:31 vpxdec
-rwxrwxrwx root root 509056 2014-07-18 07:59 vpxdec-cpu
drwxrwxrwx root root 2010-01-01 23:10 CPlusPlus
-rw-rw-rw- root root 2 2014-08-07 01:54 PPAMonitorPort.pt
-rwxrwxrwx root root 153916 2014-04-17 08:33 libGLES_mali1.so
-rwxrwxrwx root root 10304 2014-04-17 08:42 libGLESv2Agent.so
-rwxrwxrwx root root 890744 2014-04-17 08:33 libppa.so
-rwxrwxrwx root root 259484 2014-04-17 08:34 librsagent.so
-rwxrwxrwx root root 471328 2014-04-17 08:33 libtunnel.so
drwxrwxr-x root root 2010-01-01 00:11 vp9Input
-rw-rw-rw- root root 24285 2014-08-07 02:30 vp9_inter_pred_4x4.cl
-rw-rw-rw- root root 40239 2010-01-01 23:56 vp9kernel.bin
-rwxrwxrwx root root 510012 2014-08-07 02:31 vpxdec
-rwxrwxrwx root root 509056 2014-07-18 07:59 vpxdec-cpu

posted @ 2014-08-07 14:00  Magnum Programm Life  阅读(1446)  评论(0编辑  收藏  举报