[英]Call a hidden shell script file in .bashrc file in ubuntu
我創建了一個shell腳本,並將其放置在主目錄中。 但為了使其不可見,我在DOT的名稱前加上了DOT。 我的意思是.filename.sh
。 現在,我想通過.bashrc
文件調用此文件,該文件也是不可見的並放置在主目錄中。
我試過了
sh .filename.sh
和sh filename.sh
都成功,但是沒有成功。
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
. filename.sh #I'm trying to call this script here at the beginning of the file
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
...
... #some more content in .bashrc file
...
每當我們在終端中打開新標簽頁或窗口時,此shell腳本都會在終端上打印一些內容。
編輯:我嘗試過. .filename.sh
. .filename.sh
但是它僅在我們位於主目錄中時才有效(我user@user$
),但它應適用於所有目錄,我的意思是無論我身在何處(例如,假設我位於桌面user@user:~/Desktop$
),現在,如果我打開一個新選項卡,它將顯示錯誤。 它應該工作,因為當我將相同的內容直接放在.bashrc文件中而不是調用此sh文件時,它對所有目錄都適用。
如果要源文件,可以使用兩種不同的符號。 你可以寫
source path/to/filename
但是你也可以寫
. path/to/filename
從手冊 :
“。文件名[參數] ...此內置函數等效於源代碼。”
也許在您的情況下,您對點符號感到困惑。 通過在文件前面加上一個點,可以使它成為隱藏文件。 這意味着您需要的是文件名前面的第二個點,或者只是使用source
命令,我認為這是更好的表示法,更容易遺漏。 :)為了安全起見,還應該包括路徑並使用引號,以防文件名中的字符必須正常轉義。
因此,考慮到您的文件存在於主目錄的根目錄中,因此應執行以下操作。 我添加了一個測試來檢查其存在:
import="~/.filename.sh"
if [[ -f "$import" ]]; then
source "$import"
else
echo "Could not source ${import}. File does not exist."
fi
如果.bashrc和腳本位於同一目錄中,請嘗試使用以下內容
. ./.filename.sh
如果主目錄中存在腳本,則使用此命令
. ~/.filename.sh
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.