0

I have written a very simple script which takes a string as input and replace the input text in a file via sed. But I get an error always

sed: -e expression #1, char 40: unknown option to `s'

Shell script is as below

sed -i "s/c9/$1/g" Test.java 

and to run

./code.sh "lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-email\\\"]\")).clear();\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-email\\\"]\")).sendKeys(username);\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-password\\\"]\")).clear();\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-password\\\"]\")).sendKeys(password);\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"loginForm\\\"]/div[3]\")).click();\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-identity-domain\\\"]\")).clear();\n                        lmc.driver.findElement(By.xpath(\"//*[@id=\\\"login-identity-domain\\\"]\")).sendKeys(tenantId);\n                        
lmc.driver.findElement(By.xpath(\"//*[@id=\\\"loginBtn\\\"]\")).click();"
je2tdam
  • 3
  • 1

1 Answers1

0

The string contains unescaped //. After inserting the string the first one will close the substitution, the second will be the unknown option.

You either have to escape the slashes with backslashes to sed (which need to be escaped to the shell, so you need double-backslashes, resulting in ugly\\/\\/ or better use a different delimiter to the s command which is not part of the string like

sed -i "s_c9_$1_g" Test.java

This way the slashes lose their special meaning.

Philippos
  • 13,453