0

I have a cache in the filesystem which I implemented this way.

  1. To store a value I do something like

    echo ""$KEY1"_"$KEY2"="$VALUE" >> $CACHE_DIR/$INDEX.cache
    
  2. To get a value, first I source the cache file:

    source $CACHE_DIR/$INDEX.cache
    

    and then I echo the "$KEY1"_"$KEY2"

Cache example:

foo1_foo2=wohohhowwho

The problem with this, is that I have some keys that have a "/" in it, so when I have this:

foo3_foo/4=wohohhowwho

and source the file, it says

cache/15637.cache: line 1: foo3_foo/4=wohohhowwho: No such file or directory

because of the /.

Is there an option to the source command to not search in the path for files and only take in count the content as vars? I could escape the /, but is there another way?

4

2 に答える 2

2

/は bash 変数の文字として許可されていないため、他に方法はありません。

于 2012-10-24T16:06:50.293 に答える
1

bash は変数名 (文字、数字、および下線文字) のみをサポートするため、キーを「サニタイズ」する必要があります。

KEY1=${KEY1//\//_}
KEY2=${KEY2//\//_}
于 2012-10-24T16:50:57.643 に答える