`
收藏列表
标题 标签 来源
shell基础的运用 linux基础运用 linux shell脚本基础
#!/usr/bin/env bash
# source命令从文件中读入 bash 语句,然后执行它们
source config.properties
export myname='this my first linux shell!'
export i='100'

echo $title

java -version

echo ${JAVA_HOME}

helloshell(){
	# 局部变量
	local version="1.0"
	# 全局变量
	name="phl"
	echo "hello shell!"
}

helloshell 

echo $version
echo $name

echo ${myname}


# 这个语句的格式很严谨 if空格[空格a空格=空格b空格]
if [ "$1" = "h" ]
then
	echo this is piao hailin.
	echo the first parameter is $1
	echo $((1000/3))
	echo "这是一个特殊的参数,第0号索引:$0"
	echo '参数的个数是:'$#
	#m是循环控制变量
	for m in $@
	do
		echo  "$m"
	done
elif [ "$2" = "p" ]
then
	echo 'exit the shell.'
	exit
else
	echo this is not piao.
fi

echo 'while'
myvar=0
while [ $myvar -ne 3 ]
do
	echo $myvar
	myvar=$(( $myvar + 1 ))
done

echo 'until'
myvar=0
until [ $myvar -eq 3 ]
do
	echo $myvar
	myvar=$(( $myvar + 1 ))
done
 
case "${1}" in
	gz)
		echo 'this is a gz.'
		;;
	tar)
		echo 'this is a tar.'
		;;
	*)
		echo "this is an other."
		exit
		;;
esac      
shell
小布老师视频:
http://www.boobooke.com/bbs/viewthread.php?tid=7630&extra=page%3D1

linux命令 网址:
http://linux.chinaitlab.com/special/linuxcom/


這是怎麼回事兒呀?bash: ./abc.sh: cannot execute binary file

好像裡面有非法字符 :?:
這樣試試:
$at file
#!/bin/bash
echo $PATH

$bash file


当shell脚本中含有非法字符时,会出现上述错误。所以在编写shell脚本时,最好不好直接粘帖,想要粘帖,可以用文本的形式粘帖,例如

#!/bin/sh

在脚本刚开始一行中,粘帖中会吧“!”字符的格式也相应粘帖过来,从而导致上述错误提示,使用vi命令修改后,可以正常运行脚本


在sh文件中加入一行空行就ok了 



  if (proc.waitFor() != 0) {   
            logger.error("ִcmd [" + cmd + "] and returen is " + proc.exitValue());   
            return false;   
        } else {   
            return true;   
        } 






java.io.IOException: ./shell.sh: cannot execute
        at java.lang.UNIXProcess.fullPath(UNIXProcess.java:366)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:172)
        at java.lang.ProcessImpl.start(ProcessImpl.java:114)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:466)
        at java.lang.Runtime.exec(Runtime.java:607)
        at java.lang.Runtime.exec(Runtime.java:480)
        at com.huawei.fms.probe.service.common.FtpProtocol.downLoadUnzip(FtpProt
ocol.java:310)
        at com.huawei.fms.probe.service.common.FtpProtocol.download(FtpProtocol.
java:203)
        at com.huawei.fms.probe.service.common.FtpProtocol.main(FtpProtocol.java
:62)




JavaShellUtil javaShellUtil = new JavaShellUtil();
//参数为要执行的Shell命令,即通过调用Shell脚本sendKondorFile.sh将/temp目录下的tmp.pdf文件发送到192.168.1.200上
int success = javaShellUtil.executeShell("sh /tmp/sendKondorFile.sh /temp tmp.pdf");


经过研究,正确的程序如下:
public class test{
public static void main(String args[]){
Runtime rt=Runtime.getRuntime();
String str[]={"/bin/sh","-c","sh test.sh"};
Process pcs=rt.exec(str);
BufferedReader br = new BufferedReader(new InputStreamReader(pcs.getInputStream()));
String line=new String();
while((line = br.readLine()) != null)
{
System.out.println(line);
}
try{
pcs.waitFor();
}
catch(InterruptedException e){
System.err.println("processes was interrupted");
}
br.close();
int ret=pcs.exitValue();
System.out.println(ret);

}
}
Global site tag (gtag.js) - Google Analytics