0

What I'm trying to do is the following. My script reads the name of folders in current directory:

for D in */ ; do echo $D done

The result is:

folder1/ folder2/ folder3/

After this I can use the $D for other purposes. I have another variable : PATH What I want to do is make variable PATH dependent of the $D in a form of a list or something.

Like if $D is folder1/ then PATH is /var/lib/idk

if $D is folder2/ then PATH is /home/lib/user

if $D is folder3/ then PATH is something completly different

Then i'd like to use $PATH in an svn checkout like this

svn checkout svn://svn.something.local/home/idk/$PATH

I've red that it's possible with array, but the examples are too compicated for someone on my level. Can someone make a more simple example?

Thanx in advance

  • 1
    You're modifying `$PATH`? I ***strongly*** suggest you use another name, e.g. lowercase `$path`. – iBug Apr 09 '18 at 08:47

2 Answers2

0

First of all, uppercase variable names are not recommended, and PATH is a perfect example of why not!

Since you tagged Bash, you could use an associative array:

#!/bin/bash

declare -A paths
paths=( 
  [folder1/]=/var/lib/idk
  [folder2/]=/home/lib/user
  [folder3/]='something completely different' 
)

for dir in */; do
  path=${paths[$dir]}
  echo svn checkout svn://svn.something.local/home/idk/$path
done

Remove the echo if you're happy with the output.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
0

Use case:

case $D in
  folder1*)
    path=/var/lib/idk
    ;;
  folder2*)
    path=/home/lib/user
    ;;
  folder3*)
    path=some other thing
    ;;
  *)
    echo "Error, Unknown variable D"
    ;;
esac
iBug
  • 35,554
  • 7
  • 89
  • 134