Bash - Source a file with "/" - file

I have a cache in the filesystem which I implemented this way.
To store a value I do something like
echo ""$KEY1"_"$KEY2"="$VALUE" >> $CACHE_DIR/$INDEX.cache
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?

There is no other way since / is not allowed as a character in bash variables.

you have to ``sanitize'' you key as bash only support for variable name: letter, digit and the underline character.
KEY1=${KEY1//\//_}
KEY2=${KEY2//\//_}

Related

Vim: how to append the content of a variable to a file?

I was wondering: is it possible to write the content of a variable (in my case, the last search) to a file with a command?
I tried the following:
:echo #/ >> /tmp/foo.txt
:#/w /tmp/foo.txt
But that didn't work. Any idea on what is the correct way to do this?
An alternative solution to romain’s proposal is to use the redir command which redirects messages to a file. As described in :help redir,
The messages which are the output of commands are written to that file,
until redirection ends.
To append the contents of the search register, run the following sequence of commands:
redir >> /tmp/foo.txt
echo #/
redir END
This sequence could be turned into a function and/or used as a key mapping.

How do I delete text before a pattern using a batch script

Using a batch script, how do I delete the following pattern from the start of a file?
DN: CN=ACME Test,OU=ACME Test Company,O=ACME Big Org someObject;binary:
There is a space character after binary: that needs deleting too.
The file contains:
DN: CN=ACME Test,OU=ACME Test Company,O=ACME Big Org someObject;binary: MI...
The ... indicates lots more character after the MI
I just need the MI and all the characters the follow it to be in the file after the batch script has run
I think what you are looking for is answered here
Batch script to find and replace a string in text file without creating an extra output file for storing the modified file
but instead of taking in the "replace=%2" as an argument and replacing with that, you can just replace with an empty string (or pass an empty string in as the parameter if it will never change)
with dcod's link you would have to know the first part of the string to replace it with nothing. His link is nearly fine, but to delete from the start to (including) binary;, instead of
set "line=!line:%search%=%replace%!"
use
set "line=!line:*binary: =!"

What does ECHO .> mean in batch file?

I have a batch file with the following code within:
ECHO .> C:\file.txt
I read about ECHO and understand what it is used for, but what I do not know is what are the characters used for after the word echo (.>) and what is the use of the path of file after that.
It's used to truncate, or create if necessary, the file. echo . outputs a single line which is redirected to the file, effectively truncating it.
To obtain an absolutely empty file I often use cd . > filename. I don't know where I picked that one up but it's been around for a long time on UNIX systems.
> redirects the output of the command before.
echo .>c:\file.txtprints a dot to the file c:\file.txt, overwriting its contents (so it will contain a dot only afterwards)
I think, you got it wrong. Usually echo.>file.txt is used to create a empty file (or delete the contents, if the file exists). (Note the missing space)

How to remove specific characters from a file name?

I have bunch of files that need to have a (.) dot removed from the file-name. Eg. "Mr.-John-Smith.jpg" to "Mr-John-Smith.jpg". I don't have real experience with programming and know only html/css and a little javascript. I found another identical question here on stackoverflow, but what I gathered it was fixed on linux system and BASH was used.
Anyways, if anyone could provide me a tutorial on which program to use for this and what code to execute in that program to make it happen I'd be grateful.
if you are using a windows environment (which i guess you do)
you can download this free utility to mass change file names !
main page :
http://www.bulkrenameutility.co.uk/Main_Intro.php
download page :
http://www.bulkrenameutility.co.uk/Download.php
its easy to use
enjoy
If your file names in a file...
1- Open Microsoft Word or any text editor. Press ctrl+h and then search "." without quotes then replace it with blank character.
2- It will remove all dots, again bring "." to your file extention such as .jpg , .png searh your file extention for example "jpg" and replace it with ".jpg"
It will works %100, i am using this method everytime.
if they are not in a file and if you want do somethings in your operation systems' file system
Try this program. It is very useful for this operation;
Download
To remove all except the extension dot from all files in a directory, you can use PowerShell that comes with newer versions of Windows, and can be downloaded for older versions;
Line breaks inserted for readability, this should go on one line;
PS> dir | rename-item -newname {
[System.IO.Path]::GetFileNameWithoutExtension($_.name).Replace(".","") +
[System.IO.Path]::GetExtension($_.name); }
What it does is to take the file name without an extension and remove all dots in it, and then add back the extension. It then renames the file to the resulting name.
This will change for example do.it.now to doit.now, or in your case, Mr.-John-Smith.jpg to Mr-John-Smith.jpg.

filename contains space and wildcard in a variable

I receive files which names contain spaces and change every week (the name contains the week number)
IE, the file for this week looks like This is the file - w37.csv
I have to write a script to take this file into account.
I didn't succeed in writing this script.
If I do :
$FILE="This is the file - w*.csv"
I don't find /dir/${FILE}
I tried "This\ is\ the\ file - w*.csv"
I tried /dir/"${FILE}" and "/dir/${FILE}"
But I still can't find my file
It looks like the space in the name needs the variable to be double-quoted but, then, the wildcard is not analysed.
Do you have an idea (or THE answer)?
Regards,
Olivier
echo /dir/"This is the file - w"*.csv
or — you almost tried that —
echo /dir/This\ is\ the\ file\ -\ w*.csv
Use a bash array
v=( /dir/This\ is\ the\ file - w*.csv )
If there is guaranteed to be only one matching file, you can just expand $v. Otherwise, you can get the full list of matching files by expanding as
"${v[#]}"
or individual matches using
"${v[0]", "${v[1]}", etc
First of all, you should not use the dollar sign in an assignment.
Moreover, wildcard expansion is not called in an assignment. You can use process substitution for example, though:
FILE=$(echo 'This is the file - w'*.csv)
Note that the wildcard itself is not included in the quotes. Quotes prevent wildcard expansion.

Resources