I have been instructed to make a javascript. after learning some shell script (very minimal). I have created the following.

for item in *
    if [ -f $item ]
        sed  's/test/open/g' file.sh

This program I believe looks for files within the folder I have placed this program in. And afterwards finds file.sh and replaces the word test with open, from what I understand from the sed (again forgive me if it is not true). How can I modify this so instead of one file this program looks through all of the .sh files in the program and finds the word test in all of them and replaces it with open. I'm sorry this is vague if so but this is all I have to work with.

closed as too broad by xhienne, maulinglawns, Stephen Rauch, αғsнιη, steve Aug 12 '17 at 20:30

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    This is really too vague – xhienne Aug 12 '17 at 19:00
  • sorry what can I clarify\ – bobbysamuels Aug 12 '17 at 19:13
  • Take the time to got to the help center and see what we expect from a good question. From what I currently read, there is absolutely no detail that could help me make a better answer than your "[you] have to use an if statement and grep and sed commands" – xhienne Aug 12 '17 at 19:23
  • can you offer any guidance now please – bobbysamuels Aug 12 '17 at 19:43

Your script is flawed in a few ways.

for item in * <--do the following action on every item in this directory

         if [ -f $item ]  <--if the item is a file then do this
                sed  's/test/open/g' file.sh  <-- replace "test" with "open" in the file - file.sh
          <--missing closing "fi" statement here

I'm not exactly sure what you are trying to do but if you only want to replace every occurrence of test with open in the file: file.sh you should probably just do sed 's/test/open/g' file.sh otherwise if you want to replace it in every file do this:

script=$(basename "$0")
for item in "/path/to/files/"*; do
    if [[ "$item" == "$script" ]]; then
    elif [[ -f "$item" || "${item: -2}" = "sh" ]]; then 
        sed -i 's/test/open/g' "$item"


If you aren't sure what you are doing you should be really careful with this script. Make sure you point to the correct directory and be aware that you are just blanket modifying every file in it.

  • sed: couldn't open file test/open/g: No such file or directory. is the error that apppears what I wish to do is in each .sh file replace the word test with open(provided each of the files have this word). – bobbysamuels Aug 12 '17 at 20:41
  • If you do sed --version what does it return? – Jesse_b Aug 12 '17 at 20:51
  • it says 4.4.... – bobbysamuels Aug 13 '17 at 8:22
  • for item in /c/RB/* do if [ -f $item ] then echo $item && sed -i 's,{connections.url},wsami250058-gyr:8080,g' $item fi done – bobbysamuels Aug 13 '17 at 8:54
  • that was the final program required thanks for help – bobbysamuels Aug 13 '17 at 8:54

Let us first clear up some misunderstandings:

What does your script currently do? It iterates through each item in the directory where the script is executed (not where it is placed). Then it looks for each item, whether it is a file or not. If it is a file, then it opens the file file.sh and does replacement stuff using sed.

What it is supposed to do:

Instead of open file.sh after each true if statement it should open the actual item.

What you can change to accomplish that:

Replace file.sh in the sed command by $item

  • Instead of a simply downvoting the answer I would appreciate constructive criticism to improve the answer. Thank you. – fragwürdig Aug 13 '17 at 16:01

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