码迷,mamicode.com
首页 > 系统相关 > 详细

菜鸟--shell脚本编写之解决问题篇

时间:2017-04-13 13:00:33      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:for   app   ash   class   tmp   port   --   shel   本地   

 一、执行时发现adb shell进入设备后不再继续往下执行了

adb shell
cd /system/plugin/
....
exit

在网上查到的都是bat文件调用adb shell,没有sh文件调用adb shell不往下执行的,查找其他无果。

最后采用这个方法

adb shell < 1.txt

在1.txt里输入想在设备里运行的命令,可达到预期。

二、1.txt中返回${res} ,想在父脚本中引用${res} 

解决办法:父shell和子shell之间的变量传递

①如果想在子shell中调用父shell中的变量,在父shell中export变量就行

#!/bin/bash

exp8temp="hello shell"

echo $exp8temp

 

②如果是想在父shell中调用子shell中的变量,方法有很多,可参考:http://blog.csdn.net/dreamcoding/article/details/8519689/

我的情况比较特殊,是设备里运行的参数要在设备外使用,使用了命令替换和命名管道都不管用,使用中间文件法可解决。

先把在设备上运行的参数存在设备临时文件tmp.txt,退出脚本后,把tmp.txt pull到本地,再在本地 read res < tmp.txt

adb pull /system/plugin/temp.txt .

read res < temp.txt

echo $res

很笨的方法,应该有更好的办法,目前还处于菜鸟级别,希望每天进步一点,以后再回头来看有无更好解决办法。。

三、将命令结果存在文件中

res=`ls | grep appname`
echo "$res" > temp.txt

四、查询到某个进程,并kill掉

ID=`ps | grep "abc" | awk {print $2}` 
echo $ID 
echo "---------------" 
for id in $ID 
do 
kill -9 $id 
echo "killed $id" 
done 
echo "---------------"

 

菜鸟--shell脚本编写之解决问题篇

标签:for   app   ash   class   tmp   port   --   shel   本地   

原文地址:http://www.cnblogs.com/biang/p/6700876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!