2012-08-27 63 views
3
#!/bin/bash 
# This file will fix the cygwin vs linux paths and load programmer's notepad under windows. 
# mail : <[email protected]> 
# invokes the GNU GPL, all rights are granted. 


# check first parameter is non empty. 
# if empty then give a error message and exit. 
file=${1:?"Usage: pn filename"}; 

if [[ "$file" == /*/* ]] ;then 
    #if long directory name. 
# :FAILTHROUGH: 
    echo "$0: Executing pn.exe $file" 

else 
    file="$(pwd)/$file"; 
fi 

#check whether the filename starts with/if so replace it with appropriate prefix # 
prefix="C:/cygwin/"; 

#check for the partterns starting with "/" # 
echo $var | grep "^/*$" 
if [[ "$?" -eq "0" ]] ;then 
    # check again whether parttern starts with /cygdrive/[a-z]/ parttern # 
    if [[ $file == /cygdrive/[a-z]/* ]] ; then 
    file=${file#/cygdrive/[a-z]/}; 
    file="C:/"$file; 
    else 
    file="$prefix""$file"; 
    fi 
fi 


#check for the appropriate file permissions # 
# :TODO: 


echo $file 
exec "/cygdrive/c/Program Files (x86)/Programmer's Notepad/pn.exe" $file 

正如我在我的計劃,轉換Cygwin和窗口和負載 的pn.exe [程序員在Windows記事本]之間的路徑名。所以我的問題是,殼體試驗運營商正則表達式

  1. 有「[[」或「測試」運算符的正則表達式內置。 (以及我在上述程序中使用它們的 )。但是,爲什麼如果我改變他們沒有在這裏工作,

    echo $var | grep "^/*$" 
    if [[ "$?" -eq "0" ]] ;then 
    

    此,

    if [[ "$file" == ^/*$ ]] ;then 
    

    是什麼原因呢?有什麼解決方法嗎? 我已經嘗試過第二種方法[[ "$file" == ^/*$ ]]但它沒有工作。 然後,簡單的谷歌搜索帶給我這裏:http://unix.com/shell-programming

  2. 如何找到[[operator或'test'命令的所有文檔?我已經使用 man test但:(哪位文件規定了它的侷限性對正則表達式的使用,如果有這樣。

在先進的感謝。

+0

的可能重複[使用中,如果正則表達式在bash中的條件](http://stackoverflow.com/que stions/2348379/use-regular-expression-in-if-condition-in-bash) – tripleee

回答

4

首先,grep "^/*$"將只匹配像含路徑只有斜槓「/ 」,‘///’,‘////’您可以使用grep "^/"匹配開始以斜線的路徑如果你想使用bash的正則表達式:

var="/some" 
#echo $var | grep "^/" 
if [[ "$var" =~ ^/ ]] ;then 
    echo "yes" 
fi 
+0

對cygwin sehll起作用的Ya,但問題仍然存在,我想我錯過了一些東西。你的意思是'[[「$ var」=〜^ /]],那是什麼意思?在哪裏找到規範? –

+0

oky先生,它的bash建在定期比較運營商在這裏: http://www.linuxjournal.com/content/bash-regular-expressions 感謝您的想法。 –

+0

oky我知道了http://stackoverflow.com/questions/2348379/use-regular-expression-in-if-condition-in-bash:抱歉問:可能的複製: –