0

I want to write a bash script that takes a user input (which will be a filename) and replaces a path to a file inside a css file with that filename. For simplicity, the two files will be in the same folder and in the css code only the filename at the end of the path should be changed.

I thought of using regex to match any line of code that has a specific pattern and then change the end of it. I know about sed, but since the filename always changes I'm not sure how to solve this problem other than regex. I also thought of adding a variable in the css file that holds the filename as a value and then adding that variable at the end of the path, but I'm not sure then how to access that variable from a bash script.

Any recommendations on how to tackle this problem?

Thanks!

Edit Adding more Information: Here is the line in the css file I want to edit. The part to be changed is the fileName.png at the end. Since it will change I thought of using a regex to "find" the correct spot in the css file.

background: #2c001e url(file:////usr/share/backgrounds/fileName.png/);

A regex matching only this line in this specific file is the following. It could probably be simplified, but I don't see a reason why since it should work too:)

(background)\:\s\#.{6}\s(url)\((file)\:\/{4}(usr)\/(share)\/backgrounds\/.+\.(png)\/\)\;

2 Answers2

0

So, there are some ways to do that. You can check topic in links below. sed command is also good idea. But before executing it, you can build a new variable (or multiple variables) to use them in regex sed -e syntax.

Maybe, if you will add some input and output examples, I could be more specific in this case.

Atty
  • 1
  • 3
  • Thanks! I added some more information so it might be easier. I tried the sed command the following way as suggested by @CrippledTable, but I'm not quite sure what TemplateFile is and fileToUse. `sed "s/{(background)\:\s\#.{6}\s(url)\((file)\:\/{4}(usr)\/(share)\/backgrounds\/.+\.(png)\/\)\;{/$background/g" templateFile >fileToUse` – CharlyChatlin Sep 24 '19 at 09:40
0

To replace the input in the file at run-time you could use this line in a script

sed "s/stringToReplace/$1/g" templateFile >fileToUse

the $1 is referencing the 2nd bash script argument (the first being $0, the name of the invoking script). stringToReplace would be written in verbatim in the templateFile.

You could also use a script with two runtime arguments ($1, $2), and you would change the original contents of the fileToUse using the -i option. But this requires storage of the last file path to be used as argument $1.

CrippledTable
  • 784
  • 5
  • 20
  • Thanks! I added some information to the original post and also a background variable in the script and assigned it as the one to replace. It just deletes all my text in my test file instead of replacing it with the background specified. Also: What do you mean by templateFile and fileToUse? Which one would be which? – CharlyChatlin Sep 24 '19 at 09:48
  • Well your template file would just be a template.. This is so that you always know what the string you want to replace will be. You could just put "stringToReplace" in place of "fileName.png". The fileToUse, will be the file you actually use in your css implementation. – CrippledTable Sep 25 '19 at 17:03