user1082754
user1082754

Reputation:

Prompt for file path: home directory alias and case-sensitive tab completion

I am writing a shell script that prompts the user for a file path:

read -e -p "Enter the path to the file: " FILEPATH

I am then using this file path to perform operations – namely to compress a folder.

(cd "$FILEPATH"; tar -cvz *) > /tmp/torrent.tar.gz;

At the prompt, if I use the ~ alias (home directory), then the shell script doesn't seem to understand this, as the tar function compresses the wrong path. Is there anyway I can allow for this alias?

Also, tab completion seems to be case-sensitive at the prompt. I was wondering how I can change that?

Upvotes: 1

Views: 1678

Answers (2)

perreal
perreal

Reputation: 97948

Example using eval:

read -e -p "Enter the path to the file: " FILEPATH
eval FILEPATH=$FILEPATH
cd $FILEPATH
echo $PWD

In your case it becomes:

read -e -p "Enter the path to the file: " FILEPATH
eval FILEPATH=$FILEPATH
(cd "$FILEPATH"; tar -cvz *) > /tmp/torrent.tar.gz;

To deal with spaces you can use sed:

read -e -p "Enter the path to the file: " FILEPATH
FILEPATH=$(echo $FILEPATH | sed 's/ /\\ /')
eval FILEPATH=$FILEPATH
cd "$FILEPATH"
echo $PWD

Upvotes: 1

paulmelnikow
paulmelnikow

Reputation: 17208

You could apply the substitution yourself like this:

filepath=${filepath/\~/$HOME}

I don't know whether there's a way to get the shell to do it for you.

Here's an answer to your other question: https://superuser.com/questions/90196/case-insensitive-tab-completion-in-bash

Upvotes: 0

Related Questions