jenkins執(zhí)行shell讀不到環(huán)境變量問題_第1頁
jenkins執(zhí)行shell讀不到環(huán)境變量問題_第2頁
jenkins執(zhí)行shell讀不到環(huán)境變量問題_第3頁
jenkins執(zhí)行shell讀不到環(huán)境變量問題_第4頁
jenkins執(zhí)行shell讀不到環(huán)境變量問題_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、jenkins執(zhí)行shell讀不到環(huán)境變量問題從系列中,了解到j(luò)enkins執(zhí)行shell的原理。在使用enkins過程中,發(fā)現(xiàn)執(zhí)行shell讀取不到J/etc/profilel以及用戶下.bash_profilei殳置的環(huán)境變量。環(huán)境:RedHatEnterprise5.5根據(jù)jenkins-core項(xiàng)目Shell.java的buildCommandLine方法publicStringbuildCommandLine(FilePathscript)if(commandstartsWith("#!")/interpreteroverrideintend=commandind

2、exOf('n');if(end<0)end=commandlength();List<String>argsnewArrayList<String>();argsaddAll(ArraysasList(Utiltokenize(commandsubstring(0,end).trim();argsadd(scriptgetRemote();argsset(0,argsget(0).substring(2);/trimoff"#!"returnargstoArray(newStringargssize();elsereturnn

3、ewStringgetDescriptor().getShellOrDefault(scriptgetChannel(),"-xe",scriptgetRemote();在默認(rèn)的情況下,執(zhí)行shell會在節(jié)點(diǎn)上tmp目錄生成類似hudson224519953209659762.sh后面數(shù)字根據(jù)規(guī)則生成,具體執(zhí)行的命令如:/bin/sh-xe/tmp/hudson224519953209659762.s。如果ExecuteShell里面具體命令為以下內(nèi)容:#!/bin/bash+x那么根據(jù)上面代碼,具體執(zhí)行的命令就會變成/bin/bash+x/tmp/hudson224519

4、953209659762.sh知道jenkins執(zhí)行shell的原理后,接下來我們要談?wù)劷换ナ胶头墙换ナ絟ell、登錄和非登錄shelI之間的區(qū)別什么是交互式shell(interactiveshell)和非交互式shell(non-interactiveshell)交互式的shell會有一個(gè)輸入提示符,并且它的標(biāo)準(zhǔn)輸入、輸出和錯(cuò)誤輸出都會顯示在控制臺上。這種模式也是大多數(shù)用戶非常熟悉的:登錄、執(zhí)行一些命令、退出。當(dāng)你退出后,shell也終止了。非交互式shell是bashscript.sK這類的shell。在這種模式下,shell不與你進(jìn)行交互,而是讀取存放在文件中的命令并且執(zhí)行它們。當(dāng)它讀

5、到文件的結(jié)尾EOF,shell也就終止了。什么是登錄式shell(loginshell)和非登陸式shell(no-loginshell)需要輸入用戶名和密碼的;hell就是登陸式shell。因此通常不管以何種方式登陸機(jī)器后用戶獲得的第一個(gè)hell就是loginshell。不輸入密碼的ssh是公鑰打通的,某種意義上說也是輸入密碼的。非登陸式的就是在登陸后啟動bash等,即不是遠(yuǎn)程登陸到主機(jī)這種。通過manbash了解loginshell和interactiveshell,如下INVOCATIONAloginshellisonewhosefirstcharacterofargumentzeroi

6、sa-,oronestartedwiththe-loginoption.Aninteractiveshellisonestartedwithoutnon-optionargumentsandwithoutthe-coptionwhosestandardinputanderrorarebothconnectedtoterminals(asdeterminedbyisatty(3),oronestartedwiththe-ioption.PS1issetand$-includesiifbashisinteractive,allowingashellscriptorastartupfiletotes

7、tthisstate.Thefollowingparagraphsdescribehowbashexecutesitsstartupfiles.Ifanyofthefilesexistbutcannotberead,bashreportsanerror.TildesareexpandedinfilenamesasdescribedbelowunderTildeExpansionintheEXPANSIONsection.Whenbashisinvokedasaninteractiveloginshell,orasanon-interactiveshellwiththe-loginoption,

8、itfirstreadsandexecutescommandsfromthefile/etc/profile,ifthatfileexists.Afterreadingthatfile,itlooksfor/.bash_profile,/.bash_login,and/.profile,inthatorder,andreadsandexecutescommandsfromthefirstonethatexistsandisreadable.The-noprofileoptionmaybeusedwhentheshellisstartedtoinhibitthisbehavior.Whenalo

9、ginshellexits,bashreadsandexecutescommandsfromthefile/.bash_logout,ifitexists.Whenaninteractiveshellthatisnotaloginshellisstarted,bashreadsandexecutescommandsfrom/.bashrc,ifthatfileexists.Thismaybeinhibitedbyusingthe-norcoption.The-rcfilefileoptionwillforcebashtoreadandexecutecommandsfromfileinstead

10、of/.bashrc.Whenbashisstartednon-interactively,torunashellscript,forexample,itlooksforthevariableBASH_ENVintheenvironment,expandsitsvalueifitappearsthere,andusestheexpandedvalueasthenameofafiletoreadandexecute.Bashbehavesasifthefollowingcommandwereexecuted:if-nH$BASH_ENV"then.H$BASH_ENV"fib

11、utthevalueofthePATHvariableisnotusedtosearchforthefilename.對manbash解讀:如果一個(gè)bash是交互式登錄Shell或者使用-login參數(shù)的非交互式Shell。首先會執(zhí)行/etc/profil咬件。然后按順序查按/.bash_profile,/.bash_login,-/.profile,這三個(gè)文件誰存在并且有讀權(quán)限就執(zhí)行誰,然后后面的就不會再執(zhí)行??梢酝ㄟ^指定-noprofile參數(shù)來禁止這種默認(rèn)行為。當(dāng)?shù)卿汼hell退出之后,bash會讀取/.bashogou文件并執(zhí)行。如果/.bash_profile文件存在的話,一般還會執(zhí)

12、行/.bashr(文件。因?yàn)樵?.bash_profile文件中一般會有下面的代碼:if-f/.bashrc;then./.bashrcfi/.bashrc中,一般還會有以下代碼:if-f/etc/bashrc;then./etc/bashrcfi所以執(zhí)行順序?yàn)椋?etc/profile->(/.bash_profile|/.bash_login|/.profile)->/.bashrc->/etc/bashrc->/.bash_logout如果是一個(gè)交互式非登錄Shell,bash會讀取/.bashrc文件。同時(shí),可以指定-norc參數(shù)來禁止該行為,或者通過-rcfi

13、le指定其它文件。如果是一個(gè)非交互式非登錄Shell,比如運(yùn)行一個(gè)Shell腳本,它會在環(huán)境查找BASH_ENV變量。通過上面的分析,對于常用環(huán)境變量設(shè)置文件,整理出如下加載情況表:文件非交互+登陸式交互+登陸式交互+非登陸式非交互+非登陸式/etc/profile加載加載/etc/bashrc加載加載/.bash_profile加載加載/.bashrc加載加載加載BASH_ENV加載執(zhí)行腳本,女Bbashscript.sS屬于non-login+non-interactive所以jenkins默認(rèn)情況下/bin/sh-xe/tmp/hudson224519953209659762.sh是屬于

14、non-login+non-interactive解決方案通過manbash可知:OPTIONSInadditiontothesingle-charactershelloptionsdocumentedinthedescriptionofthesetbuiltincommand,bashinterpretsthefollowingoptionswhenitisinvoked:-cstringIfthe-coptionispresent,thencommandsarereadfromstring.Ifthereareargumentsafterthestring,theyareassignedt

15、othepositionalparameters,startingwith$0.-iIfthe-ioptionispresent,theshellisinteractive.-lMakebashactasifithadbeeninvokedasaloginshell(seeINVOCATIONbelow).-rIfthe-roptionispresent,theshellbecomesrestricted(seeRESTRICTEDSHELLbelow).-sIfthe-soptionispresent,orifnoargumentsremainafteroptionprocessing,thencommandsarereadfromthestandardinput.Thisoptionallowsthepositionalparameterstobesetwheninvokinganinteractiveshell.-DAlistofalldouble-quotedstringsprecededby$isprintedonthestandardoutput.Thesearethestringsthataresubjecttolanguagetranslationwhe

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論