2

I'm using the following code to try to clean up some html files. $1 should be the name of a directory. This works fine from the command line and the requested changes are made to the files in the directory. echo displays the $directory in both cases, however nothing is changed in the specified directory when used from the bash script. Why? How do I fix this?

#!/bin/bash
directory=$1
echo $directory
LC_CTYPE=C
sed -i.clean -f uncap-html.sed $directory/*.html
echo DONE

closed as off-topic by don_crissti, Jeff Schaller, GAD3R, countermode, roaima Feb 13 '17 at 22:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – don_crissti, Jeff Schaller, GAD3R, countermode, roaima
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Are you trying to reference $1 (in the context of the shell script) within uncap-html.sed? – DopeGhoti Feb 13 '17 at 20:17
  • no i'm just using $1 ($directory) to specify the folder that sed is acting upon. – kovach.j Feb 13 '17 at 20:34
  • Don't have a clue what I did but the above code is now working as desired. – kovach.j Feb 13 '17 at 20:49
  • Please get into the habit of double-quoting your variables: directory="$1" and echo "$directory" and sed ... "$directory"/*.html – roaima Feb 13 '17 at 21:51
1

I am not quite sure why it is failing for you when same script works as expected for me.

sed.sh

  1 #!/bin/bash
  2 directory=$1
  3 echo $directory
  4 LC_CTYPE=C
  5 sed -i.clean -f uncap-html.sed $directory/*.html
  6 echo DONE

uncap-html.sed

[gc13@oc1245342277 sh]$ cat uncap-html.sed 
s/z/o/g
[gc13@oc1245342277 sh]$ 

1.html

Hello

2.html

World

Execution

[gc13@oc1245342277 sh]$ cat *.html
Hellz
Wzrld
[gc13@oc1245342277 sh]$ 

You may want to try another approach:

#!/bin/bash
FILES="$1/*.html"
echo $FILES
LC_CTYPE=C
sed -i.clean -f uncap-html.sed $FILES
echo DONE
  • Thanks, it's working now - go figure. Am clueless what i did differently. – kovach.j Feb 13 '17 at 20:59
  • Great. It must be an error of single character misplaced. – GC 13 Feb 13 '17 at 21:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.