调用shell脚本 怎么像调用java方法那样传入参数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用shell脚本 怎么像调用java方法那样传入参数相关的知识,希望对你有一定的参考价值。

下面是shell脚本test.sh内容,现在需要做的是folder和vref_file这两个值可以作为参数传入,而不固定写死。该如何实现?

folder="/home/jack/files/portal/uservisit"
vref_file="/home/jack/files/portal/uservisit/a_10000_20150415_VGOP1-R2.10-78204_00.vref"
: > $vref_file
for file in $folder/*.dat; do
temp_file=$file
filesize=`wc -c < $temp_file`
row=`wc -l < $temp_file`
file_name=$temp_file##*/
data_date=$file_name:8:8
modify_date=`ls --full-time $temp_file | cut -d" " -f6-7 | cut -c1,2,3,4,6,7,9,10,12,13,15,16,18,19`
echo $file_name"€"$filesize"€"$row"€"$data_date"€"$modify_date >> $vref_file
done

上面要做的是获取某个目录下以.dat结尾的文件名 文件大小 文件行数 最近修改时间,以前没接触过shell这
块,不知道有没有更加简便的方法。求大神指点,谢谢!

folder="$1"

ver_file="$2"

fodler和ver_file分别是调用 test.sh时的第一个和第二个参数,如下调用

test.sh "/home/jack/files/portal/uservisit"  "/home/jack/files/portal/uservisit/a_10000_20150415_VGOP1-R2.10-78204_00.vref"

参考技术A $@: 参数列表, $1地一个参数.......

或者想高大上点。。。用getopts

java 调用 shell 脚本

项目需求做一个视频传输,需要一个例子java调用shell脚本

参考技术A 在写程序时,有时需要在java程序中调用shell脚本,可以通过Runtime的exec方法来调用shell程序,运行脚本。每个Java 应用程序都有一个Runtime 类实例,使应用程序能够与其运行的环境相连接。通过Runtime对象可以返回运行环境的情况,包括CPU数,虚拟机内存大小等,并能够通过exec方法调用执行命令。可以通过getRuntime 方法获取当前Runtime实例。 public boolean ExeShell() Runtime rt = Runtime.getRuntime(); try Process p = rt.exec(checkShellName); if(p.waitFor() != 0) return false; catch (IOException e) SysLog.error("没有找到检测脚本"); return false; catch (InterruptedException e) e.printStackTrace(); return false; return true; 其中p.waitFor()语句用来等待子进程结束,其返回值为进程结束退出码。

以上是关于调用shell脚本 怎么像调用java方法那样传入参数的主要内容,如果未能解决你的问题,请参考以下文章

java 调用 shell 脚本

Java调用JS脚本里的方法

怎么用java代码调用远程Linux上的shell脚本

java调用shell脚本,并得到shell脚本的返回值

Lua脚本怎么样调用外部脚本

IBm tws调用shell