Shell regexp and shell script changing url links to base64 in css files

so this script can alter the url to data64 encoding by matching in css file background-image: url(afdsf.png).

some contribute this:

Code:
 echo 'background-image: url("../images/doesntmatter/imgDoesNotMatter.png");' | sed -r 's,background-image\s*:\s*url\("(.+)"\);,\1,' ../images/doesntmatter/imgDoesNotMatter.png

Code:
#!/bin/bash
: <<'END_COMMENT'
awk -F'[()]' '

/background-image: url(.*)/ {
  cmd=sprintf("base64 -w0 %s",$2)
  cmd | getline b64
  close(cmd)
  $0=$1 "(data:image/png;base64," b64 ");"
}1' ./playerRanks_test_base64.css

awk -F'[()]' -v q="'" '

/background-image: url(.*)/ {
  cmd=sprintf("openssl enc -base64 -in %s | tr -d %c\\n%c",$2,q,q)
  cmd | getline b64
  close(cmd)
  $0=$1 "(data:image/png;base64," b64 ");"
}1' ./playerRanks_test_base64.css
END_COMMENT
awk -F'[()]' -v q="'" '

/background-image: {0,3}url(.*)*/ {
  cmd=sprintf("openssl enc -base64 -in %s | tr -d %c\\n%c",$2,q,q)
  cmd | getline b64
  close(cmd)
  $0=$1 "(data:image/png;base64," b64 ");"
  $2=$3 "(data:image/png;base64," b64 ");"
  $4=$5 "(data:image/png;base64," b64 ");"
}1' ./playerRanks_test_base64.css

what i like for it to do is match these possible run in when it is doing this:

Code:
//need help matches cases like these:

background-image: url("../images/doesntmatter/imgDoesNotMatter.png");

background-image: url("../images/doesntmatter/img0.png"), url(../images/doesntmatter/img1.png);

background-image: url("../images/doesntmatter/img0.png"), url(../images/doesntmatter/img1.png), url("../images/doesntmatter/img3.png");

but i'm having trouble with the regexp match for this to work... anyone care to help?
 
Last edited:
Back
Top