2012-11-02 29 views
0

我有這個小腳本如果用戶名和電子郵件中的git的配置存在,檢查,否則提示用戶輸入他們:運行一個循環,只要變量是空

GIT_USER_EXISTS=`git config --get-regexp 'name' | awk '{print $2}'` 
GIT_EMAIl_EXISTS=`git config --get-regexp 'name' | awk '{print $2}'` 
while [[ -z $GIT_USER_EXISTS || -z $GIT_EMAIl_EXISTS ]]; do 
    echo "User name and Email are not set in git. Please enter them now..." 
    echo "First and Last name:" 
    read gitUser 
    git config --global user.name "$gitUser" 
    echo "Email:" 
    read gitEmail 
    git config --global user.email "$gitEmail" 
done 

它不盡管工作;)

1st - 它甚至沒有進入循環首先。 我的猜測是,與grep不同,awk確實向變量插入了一些(隱藏的?)字符。 2nd - 我不確定腳本是否可以識別出現在有值,因爲我在循環之外聲明瞭變量。這個假設我錯了嗎?

編輯:

我通過使用反引號,而不是報價的變量名解決了第一個問題。 第二個問題仍然存在 - 必須比使用兩次變量聲明更好的使用?!一些東西沿着do的方向行進......而在shell中呢?

編輯#2:

這是卓有成效的更好一點,但有一個相當長的條件語句。它可能會更短嗎?

while [[ -z $GIT_USER_EXISTS || -o $GIT_USER_EXISTS || -z $GIT_EMAIl_EXISTS || -o $GIT_EMAIl_EXISTS ]]; do 
    echo "User name and Email are not set in git. Please enter them now..." 
    echo "First and Last name:" 
    read gitUser 
    git config --global user.name "$gitUser" 
    echo "Email:" 
    read gitEmail 
    git config --global user.email "$gitEmail" 

    GIT_USER_EXISTS=`git config --get-regexp 'name' | awk '{print $2}'` 
    GIT_EMAIl_EXISTS=`git config --get-regexp 'name' | awk '{print $2}'` 
done 

其實這幾乎是卓有成效的 - 它應該是「如果VAR被設置,爲空或VAR未設置」。但是,這將是太長了:)

回答

1

嘗試這樣做:

GIT_USER_EXISTS="git config --get-regexp 'name' | awk '{print $2}'" 
GIT_EMAIl_EXISTS="git config --get-regexp 'name' | awk '{print $2}'" 
while [[ -z $GIT_USER_EXISTS || -z $GIT_EMAIl_EXISTS ]]; do 
    echo "User name and Email are not set in git. Please enter them now..." 
    echo "First and Last name:" 
    read gitUser 
    git config --global user.name "$gitUser" 
    echo "Email:" 
    read gitEmail 
    git config --global user.email "$gitEmail" 
    GIT_USER_EXISTS="git config --get-regexp 'name' | awk '{print $2}'" 
    GIT_EMAIl_EXISTS="git config --get-regexp 'name' | awk '{print $2}'" 
done 
  • 如果測試$GIT_USER_EXISTS$GIT_EMAIl_EXISTS,你應該某處聲明它;)
+0

其實我這樣做,但認爲可能有比聲明變量兩次更好的方法。 (類似的事情......而不是......雖然......但我沒有在shell中找到任何引用)。此外,它不能解決我的第一個問題... :) – CrimsonKing

+0

呃,我發現它爲什麼沒有進入循環 - 我沒有使用反引號,但引號。 – CrimsonKing