help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] No tilde expansion in [[..]] when reading input with rea


From: John Kearney
Subject: Re: [Help-bash] No tilde expansion in [[..]] when reading input with read
Date: Tue, 26 Feb 2013 04:37:49 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3

Am 25.02.2013 02:08, schrieb Chris Down:
Here it is with the dependency on `awk' removed (no external dependencies):

    tildeExpand() {
        local path=$1

        if [[ $path =~ ^~$ || $path =~ ^~/ ]]; then
            path=${path/\~/$HOME}
        elif [[ $path =~ ^~([[:alnum:]]+) ]]; then
            user=${BASH_REMATCH[1]}
            while IFS=: read -r pUser _ _ _ _ pHome _; do
                if [[ $user == $pUser ]]; then
                    userHome=$pHome
                    break
                fi
            done < /etc/passwd
            if [[ $userHome ]]; then
                path=${path/\~$user/$userHome}
            fi
        fi

        printf '%s\n' "$path"
    }

Again, any suggestions for improvement welcome.

Best,

Chris

untested just example of how you could avoid most of the new bash specific stuff you used. these operations should also be cheaper.
  tildeExpand() {
        local path pHome pUser userHome
	case ${1%%/*} in 
	  ~) path="${HOME}${1#~}";;
	  [!~]* | ~*[![:alnum:]]*) path="${1}" ;;
	  *)
	    user=${1%%/*}
	    user=${user#~}
            while IFS=: read -r pUser _ _ _ _ pHome _; do
                if [[ $user == $pUser ]]; then
                    userHome=$pHome
                    break
                fi
            done < /etc/passwd
            if [[ $userHome ]]; then
                path=$userHome${1#~$user}
            fi
		;;
	esac

        printf '%s\n' "$path"
    }

--
View John
          Kearney's profile on LinkedIn John Kearney

reply via email to

[Prev in Thread] Current Thread [Next in Thread]