-1

I am attempting my first bash script. I would like to prompt the user to find out where they would like a cloned repo to be saved.

Currently I am assigning it like this.

warpToLocation="${HOME}/apps/"

Is there some way to do it like this:

read -e -p "Enter the path to the file: " -i "${HOME}/apps/" FILEPATH

but save the result as warpToLocation?

EDIT:

#!/bin/bash

echo "where would you like to install your repos?"

read -p "Enter the path to the file: " temp
warpToLocation="${HOME}/$temp/"

warpInLocations=("git@github.com:cca/toolkit.git" "git@github.com:cca/sms.git" "git@github.com:cca/boogle.git" "git@github.com:cca/cairo.git")



echo "warping in toolkit, sms, boogle and cairo"
for repo in "${warpInLocations[@]}"
do
  warpInDir=$repo
  warpInDir=${warpInDir#*/}
  warpInDir=${warpInDir%.*}
  if [ -d "$warpToLocation"]; then
    echo "somethings in the way.. $warpInDir all ready exists"
  else
    git clone $repo $warpInDir
fi

done

What I had done to get that error was add the code you gave me.

The issue with that is that -e (which allows you to edit the input with arrows) and -i (a preview/optional answer) runs on bash version 4 and higher and I am running version GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12).

0

What's wrong with

read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation

?

  • so FILEPATH is a variable. i thought it was a standard.. thanks man sorry its my first time dipping into bash i am scared i destroy my os – TheLegend May 24 '13 at 14:51
  • invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] warping in toolkit, sms, boogle and cairo – TheLegend May 24 '13 at 14:53
  • I have no idea what you typed in your program, but I'm pretty sure that the command I told you works in bash. If you type something else and get error messages, it's fairly pointless to copy only the error message and hope that others can reconstruct your problem from that. – Uwe May 24 '13 at 15:05
  • that would be the result that bash gave me when i tried read -e -p "Enter the path to the file: " -i "${HOME}/apps/" warpToLocation – TheLegend May 24 '13 at 15:26
  • Your read doesn't have a -i option? Are you really using bash? Which version? bash --version – Uwe May 24 '13 at 15:30
0

It is seldom constructive to ask the user interactively for a pathname. That limits the scripts usability to interactive use and it forces the user to type in (correctly) a possibly long pathname, without being able to use variable names like $HOME or $project_dir (or whatever variables the user likes to use) and without using ~.

Instead, take the pathname to the destination directory from the command line, validate that it's a directory, and clone the Git repositories into it if the they do not already exist.

#!/bin/sh

destdir=$1

if [ ! -d "$destdir" ]; then
    printf 'No such directory: %s\n' "$destdir" >&2
    exit 1
fi

for repo in toolkit sms boggle cairo
do
    if [ -e "$destdir/$repo" ]; then
        printf 'Name %s already exists for repository %s (skipping)\n' \
            "$destdir/$repo" "$repo" >&2
        continue
    fi

    printf 'Cloning %s\n' "$repo"
    git clone "git@github.com:cca/$repo.git" "$destdir/$repo"
done

This script would be used as

./script.sh "$HOME/projects/stuff"

and could be run without user interaction from e.g. Ansible or Cron.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.