
linux創建shell腳本?linux簡單的shell編程

大家好,感謝邀請,今天來為大家分享一下linux創建shell腳本的問題,以及和linux簡單的shell編程的一些困惑,大家要是還不太明白的話,也沒有關系,因為接下來...
大家好,感謝邀請,今天來為大家分享一下linux創建shell腳本的問題,以及和linux簡單的shell編程的一些困惑,大家要是還不太明白的話,也沒有關系,因為接下來將為大家分享,希望可以幫助到大家,解決大家的問題,下面就開始吧!
如何編寫linux控制臺程序
問題太模糊,你是要編寫一個Linuxshell腳本嗎?vi(你的名字).sh就可以了vi編輯器里,按i進行編輯,寫完程序按ESC退出,輸入:wq保存并退出
如何通過java遠程調用linux命令或shell腳本
1.導入maven依賴jar包
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
2.編輯相應Java代碼
packagecom.ksy.fw.base.util;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.UnsupportedEncodingException;
importorg.apache.commons.lang.StringUtils;
importch.ethz.ssh2.Connection;
importch.ethz.ssh2.Session;
importch.ethz.ssh2.StreamGobbler;
/**
*遠程執行linux的shellscript
*/
publicclassRemoteExecuteCommand{
//字符編碼默認是utf-8
privatestaticStringDEFAULTCHART="UTF-8";
privateConnectionconn;
privateStringip;
privateStringuserName;
privateStringuserPwd;
publicRemoteExecuteCommand(Stringip,StringuserName,StringuserPwd){
this.ip=ip;
this.userName=userName;
this.userPwd=userPwd;
}
publicRemoteExecuteCommand(){
}
/**
*遠程登錄linux的主機
*@return
*登錄成功返回true,否則返回false
*/
publicBooleanlogin(){
booleanflg=false;
try{
conn=newConnection(ip);
conn.connect();//連接
flg=conn.authenticateWithPassword(userName,userPwd);//認證
}catch(IOExceptione){
e.printStackTrace();
}
returnflg;
}
/**
*遠程執行shll腳本或者命令
*@paramcmd即將執行的命令
*@return命令執行完后返回的結果值
*/
publicStringexecute(Stringcmd){
Stringresult="";
try{
if(login()){
Sessionsession=conn.openSession();//打開一個會話
session.execCommand(cmd);//執行命令
result=processStdout(session.getStdout(),DEFAULTCHART);
//如果為得到標準輸出為空,說明腳本執行出錯了
if(StringUtils.isBlank(result)){
result=processStdout(session.getStderr(),DEFAULTCHART);
}
conn.close();
session.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
returnresult;
}
/**
*遠程執行shll腳本或者命令
*@paramcmd即將執行的命令
*@return命令執行成功后返回的結果值,如果命令執行失敗,返回空字符串,不是null
*/
publicStringexecuteSuccess(Stringcmd){
Stringresult="";
try{
if(login()){
Sessionsession=conn.openSession();//打開一個會話
session.execCommand(cmd);//執行命令
result=processStdout(session.getStdout(),DEFAULTCHART);
conn.close();
session.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
returnresult;
}
/**
*解析腳本執行返回的結果集
*@paramin輸入流對象
*@paramcharset編碼
*@return以純文本的格式返回
*/
privateStringprocessStdout(InputStreamin,Stringcharset){
InputStreamstdout=newStreamGobbler(in);
StringBufferbuffer=newStringBuffer();;
try{
BufferedReaderbr=newBufferedReader(newInputStreamReader(stdout,charset));
Stringline=null;
while((line=br.readLine())!=null){
buffer.append(line+"\n");
}
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
returnbuffer.toString();
}
publicstaticvoidsetCharset(Stringcharset){
DEFAULTCHART=charset;
}
publicConnectiongetConn(){
returnconn;
}
publicvoidsetConn(Connectionconn){
this.conn=conn;
}
publicStringgetIp(){
returnip;
}
publicvoidsetIp(Stringip){
this.ip=ip;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetUserPwd(){
returnuserPwd;
}
publicvoidsetUserPwd(StringuserPwd){
this.userPwd=userPwd;
}
publicstaticvoidmain(String[]args){
RemoteExecuteCommandrec=newRemoteExecuteCommand("127.0.0.1","root","root");
//執行命令
System.out.println(rec.execute("chmod777/usr/ksybak/myshell/tomcat-fw.sh"));
System.out.println(rec.execute("/usr/ksybak/myshell/tomcat-fw.sh"));
//System.out.println(rec.execute("/usr/ksybak/myshell/tomcat-fw.sh"));
//執行腳本
//rec.execute("sh/usr/local/tomcat/bin/statup.sh");
//這個方法與上面最大的區別就是,上面的方法,不管執行成功與否都返回,
//這個方法呢,如果命令或者腳本執行錯誤將返回空字符串
//System.out.println(rec.executeSuccess("ifconfig"));
}
}
linux如何運行sh腳本
Linux中執行shell腳本的4種方法總結,即在Linux中運行shell腳本的4種方法:
方法一:切換到shell腳本所在的目錄(此時,稱為工作目錄)執行shell腳本
方法二:以絕對路徑的方式去執行bashshell腳本
方法三:直接使用bash或sh來執行bashshell腳本
方法四:在當前的shell環境中執行bashshell腳本
linuxshell腳本中如何引入當前的系統環境變量
你的export命令是給當前shell的,當前console一關,shell一退,就沒了。
你應當把它寫到每次啟動時都加載的shell腳本里(系統的/etc/bashrc或者用戶的~/.bashrc),它會在每次啟動shell時都被執行。
Unix的所有環境變量、命令行指令alias什么的,都是使用這個機制。
什么是shell腳本
shell是計算機的殼通俗一點的解釋是:
如果你要對計算機發布指令,讓計算機去按照你的意愿做事,那么你就需要告訴計算機去做什么事,怎么樣去做。一般來說你可以用像windows這樣的圖形操作系統來完成,你只需點點鼠標,敲敲鍵盤就能夠搞定了。但是在圖形下面的就是shell了,windows里面的dos語言就是一種shell。這些語言通過一定的規則和語法來對計算機進行操作,相對圖形來說比較底層。
還有很多shell,比如Linux和Unix系統中的bash、ksh、csh等等。
而shell腳本的意思就是將這些shell的命令語句集合在一個文件中,進行一些較復雜的處理。
好了,本文到此結束,如果可以幫助到大家,還望關注本站哦!
本文鏈接:http://www.wzyaohuidianqi.cn/ke/3361.html
