萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Ubuntu source 命令詳解:腳本執行與內核編譯自動化實戰

source(別名 .)在當前 Shell 環境執行腳本,使變數、函數、別名等定義即時生效,區別於 bash script.sh 開啟子 Shell。本文詳解語法、內核編譯自動化場景與 PATH 修復。

基本語法

source filename
. filename  # 等效,需空格

核心差異:source vs 直接執行

實戰:內核編譯自動化腳本

建立 /usr/src/linux-2.4.20/make_command:

make mrproper &&
make menuconfig &&
make dep &&
make clean &&
make bzImage &&
make modules &&
make modules_install &&
cp arch/i386/boot/bzImage /boot/vmlinuz_new &&
cp System.map /boot &&
vi /etc/lilo.conf &&
lilo -v

後續每次編譯只需:

cd /usr/src/linux-2.4.20
source make_command

&& 保證前一命令成功才執行下一條,避免錯誤傳播。

常見報錯修復

source: command not found
# 原因:PATH 缺失標準路徑
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

掌握 source,能將複雜操作序列封裝為可複用的環境配置腳本,大幅提升運維效率。

copyright © 萬盛學電腦網 all rights reserved
方式進程環境變數影響
source script.sh當前 Shell保留(變數/函數/別名生效)
bash script.sh / ./script.sh子 Shell子 Shell 結束即銷毀