1
17
2014
1

关于脚本

run一个工程时,往往要输入多个参数文件,我以前都是自己傻傻的一个个输入,不过现在要提高自己的level,要学会用脚本。下面用一个脚本为例:

# Script writer: Youngjae Kim (youkim@cse.,psu.edu)

#!/bin/sh

echo "Running Pagemap FTL..."
../src/disksim pagemap.parv pagemap.outv ascii ./trace/test.file 0 
grep "IOdriver Response time average:" pagemap.outv
grep "IOdriver Response time std.dev." pagemap.outv

echo "Running DFTL..."
../src/disksim dftl.parv dftl.outv ascii ./trace/test.file 0
grep "IOdriver Response time average:" dftl.outv
grep "IOdriver Response time std.dev." dftl.outv

echo "Running FAST..."
../src/disksim fast.parv fast.outv ascii ./trace/test.file 0
grep "IOdriver Response time average:" fast.outv
grep "IOdriver Response time std.dev." fast.outv

第一行#表示注释,#!/bin/sh中#!用来告诉系统它后面的参数是用来执行该文件的程序,这个意思我也不是很懂。反正脚本开头要加上这个。

脚本文件里面的用于直接使用linux下的shell用语(即指令用语)

echo 是在屏幕上输出的意思,如下:

grep 是在对应文件中找文字,如

grep "IOdriver Response time average:" fast.outv  就是在fast.outv找IOdriver Response time average

下面贴上我自己写的一个脚本:这个脚本的最下面一行就是你运行该程序时在shell里面要输入的东西。有两点要注意:

1 在windows里面编辑再移进去往往会出现回车等符号的编码方式不一致而出现错误,最好在linux里建立文件,直接在linux中编辑

2.在写好脚本后要chmod 755 run  即改变脚本的权限,使其变为可执行

 

#!/bin/sh

path="/home/test"

cd $path

for i in 1 2 3 4

do

     filename=$i"LALA.trace.csv"

     /home/test/run $filename >> /home/test/result/result.csv

done

注释>>为输出重定向,若原来有此文件,则附加写


#!/bin/sh
path="/home/sourcetrace/trace/DisplayAdsDataServer/Traces0"
cd $path
for i in 1 2 3 4 
do
filename=$i"-testtrace.csv" #对i取值要加上$
echo $filename #打印filename到屏幕上
  sourcepath="/home/trace/"$filename  #存放trace的路径,加上trace名字
  echo $sourcepath
  num=`grep -n 'EndHeader' $sourcepath | cut -d : -f 1` #对num赋值,grep 返回“行数:内容”,利用管道作为cut的输入。
  echo $num
 sed "1,${num}d" /home/Traces/$filename > /home/Traces0/$filename #去掉原trace的首num行,将去掉后的结果输出到/home/Trace0下面
  /home/run $filename $i #取一个东西都要用到 $ 符号

done
 
 另外脚本中的>>作用是追加写,而 > 是清除写。若没有文件,都会创建文件。故在脚本中你想把输出到控制台屏幕上的数据导到文件中,可以使用 >> filename。这个result只是记录显示在屏幕上的printf,若代码中有fopen("/home/outcome","w+");不会影响,outcome照样输出。
例如:
 
#!/bin/sh
for i in 1 2 3
do
	/home/run $i >> result 	
done

 

下面给出接受终端参数输入的shell脚本

该脚本命名run.sh

#!/bin/sh
blktrace -w $1 -d /dev/sda1 -o -| blkparse -i - -o $2

$1为终端第一个参数,$2为终端第二个参数

运行时

./run.sh 5 output.txt

表示运行时间为5s,对应$1;输出文件名字为output.txt,对应$2

 

awk解析脚本,由于awk语句中本身符号"$"就有含义,$1表示读入文件的第一列。这里会与shell脚本语言表示的 $1 为终端输入第一个参数混淆,故书写时要注意:

#!/bin/sh
awk '{if($5=='$1')print}' 1.txt > $2

运行时

./awk.sh 1193 out.txt

表示若1.txt文件的第五列==1193,则把满足条件的行输出到out.txt中。注意有无''

Category: 脚本语言 | Tags: | Read Count: 1490
Avatar_small
AP SSC Assignment Mo 说:
2022年9月09日 21:43

Department of Government Examinations and Secondary Education Board Andhra Pradesh has conducted the Assignment Exams multiple times in the academic year in Session-1 and Session-2 (Term-1 & Term-2). There are four exams are conducted Assignment-1, Assignment-2, Assignment-3 and Assignment-4. AP SSC Assignment Model Paper Every Class 10th Standard Student Studying in Government & Private Schools in Telugu Medium, English Medium & Urdu Medium can download the AP 10th Assignment Model Paper 2023 with answer solutions for theory, objective and bit questions. Subject experts of the board have designed and introduced the practice question bank for all Part-A, Part-B, Part-C, and Part-D exams.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com