shell腳本函數調用
❶ shell腳本里的函數怎麼調用
shell中的函數,要在定義這個函數的腳本中進行調用!
#!/bin/sh
echo_line(){
echodate
echo"Wellcometoshellfunc!"
}
echo_hello(){
echo"HelloWorld!"
}
##在這里調用,將全部代碼寫入test.sh就可以了
echo"callecho_hello"
echo_hello
echo"callecho_line"
echo_line
在命令行下執行:
$test.sh
❷ shell腳本怎麼調用其他shell腳本
在Shell中要如何調用別的shell腳本,或別的腳本中的變數,函數呢?
方法一: . ./subscript.sh
方法二: source ./subscript.sh
注意:
1.兩個點之間,有空格,千萬注意.
2.兩個腳本不在同一目錄,要用絕對路徑
3.為簡單起見,通常用第一種方法
例如:
復制代碼代碼如下:
main.sh #主腳本
subscripts.sh #子腳本,或者說被調腳本
[code]
[code]
###subscripts.sh 腳本內容如下:###
#!/bin/bash
string="Hello,World! \n"
復制代碼代碼如下:
###main.sh 腳本內容如下###
#!/bin/bash
. ./subscripts.sh
echo -e ${string}
exit 0
輸出結果:
復制代碼代碼如下:
# chmod +x ./main.sh
# ./main.sh
Hello,World!
#
❸ 在linux 用shell腳本調用C語言的執行文件,用到什麼函數,求大神幫分析一下
c調用shell為system(cmd)
shell調用c執行文件和調用普通的命令一樣,如 ls cd pwd等命令都是c寫的執行文件
❹ 參數傳遞:shell腳本調用一個帶參數的python函數
把a b c寫到文件里,如example.txt,然後shell里:cif filename <example.txt
❺ 用shell腳本編寫一個函數,調用兩個數據文件的逐行數據(每一行有兩個數據)作為變數來計算,怎麼編寫
function fname(){
...
}
while read line
do
num1=`echo $line | awk '{print $1}'`
num2=`echo $line | awk '{print $2}'`
fname $num1 $num2
done < $file
❻ 如何在shell腳本里調用另一個shell腳本
子shell變數傳遞給父shell,中間可以保存個臨時文件讓父shell去讀。下邊是一個子shell,傳遞給父shell的一個例子,僅做參考#!/bin/bash(subvar="helloshell"echo"$subvar">temp.txt)readpvar
❼ 關於shell腳本#!的說明
Unix環境下的shell腳本通常都是#!/bin/sh開頭,那麼這句描述符究竟是什麼含義呢,我試圖解答這個問題。
首先#!必須出現在shell腳本的開頭位置,後面跟一個shell解析器,比如/bin/sh,或者/bin/bash,或者/usr/bin/ksh, 等等.
我們看到 a.sh使用的可執行程序是/bin/bash,這個/bin/bash是在a.sh文件裡面通過#!指定的。
a.out是一個可執行程序(ELF格式),它可以獨自運行,但是不能通過一個shell來運行。
#!實際上就是文件的魔數(magic number)
我們知道ELF格式為文件頭4個字元是".ELF",即(0x 7f 45 4c 46),而其實字元"#!"是shell腳本文件的魔數,即(0x 23 21),因為shell腳本是文本文件,#!就是兩個可讀的魔數字元。
這個魔數是干什麼用的呢?它是被操作系統exec系列函數使用的,exec函數需要載入一個文件時,它會讀取文件開頭魔數域,如果是".ELF",那麼就是一個ELF格式的可執行文件,如果是"#!"那麼就是一個腳本文件,然後再從"#!"後面繼續讀取腳本解析器,最後調用腳本解析器可執行程序,並把腳本本身作為參數傳遞給他。
注意
當前shell會讀取文件file的魔數
我也不清楚為什麼sh file格式不能支持file是二進制可執行程序,理論上sh還是可以去分析file的魔數,從而判斷file的類型,然後做區分處理。
https://en.wikipedia.org/wiki/Shebang_(Unix)
❽ shell 腳本中後台調用函數結束
比面shell腳本文件:#===========test.sh! /bin/shecho_line(){ echo date echo "Wellcome to shell func!"}echo_hello(){ echo "Hello World!"}#======================shell調用兩函數啊我用【./test.sh echo_hello】卻沒輸我已經給test.sh加執行許可權
請問envsetup.sh邊
function gettop()
{
local TOPFILE=build/core/envsetup.mk
if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
echo $TOP
else
if [ -f $TOPFILE ] ; then
# The following circumlocution (repeated below as well) ensures
# that we record the true directory name and not one that is
# faked up with symlink names.
PWD= /bin/pwd
fi
}並沒返值
調用何獲取參數 T=$(gettop)
實際參數謝謝
❾ shell調用其他腳本函數
使用位置參數:
b.sh裡面這樣寫
#!/bin/bash
basha.sh10020
a.sh里這樣寫
#!/bin/bash
number=$1
number_1=$2
functionfun1(){
echo$1
}
functionfun2(){
echo$1
}
fun1$number
指出幾點問題:
你的原腳本里為什麼fun_get=fun1
這樣做沒什麼意義;
fun2並沒有用到。
❿ 在shell腳本中調用函數,如果函數中使用了$1這樣的參數
是調用這個函數時,傳進去的參數~~~
[root@localhost test]# bash shell.sh
haha
[root@localhost test]# cat shell.sh
#!/bin/bash
function fun {
echo $1
}
fun haha