1

I have a bash script that isn't properly working, I am new to this type of coding. Any suggestions or help would be great.

I am attempting to change email addresses of an old user with a new user across our PuTTy platform. However not all directories have a certain file, I think there may be a problem with my IF statement. Here is the script I have attempted using:

#!/bin/bash
#set -x

for dirs in *
do
  echo $dirs
  cd $dirs/
  if [ -d "Seadont"]
  then 
   sed -i 's/USER1/USER2/g'
  ls
  fi
done

My results are not correct.

5
  • 1
    One problem is that you need a space before ] Commented May 19, 2014 at 19:50
  • 1
    Second problem is that you need to pass an argument to sed Commented May 19, 2014 at 19:51
  • 1
    And don't need to run cd command since it will fail after first iteration. Commented May 19, 2014 at 19:52
  • 1
    And you don't specify a file name for sed to work on, so it will fail because the -i option cannot rename standard input. If you must use cd, use a sub-shell: ( cd "$dirs/"; ... ) so that when the sub-shell is complete, the original shell hasn't changed directory (so you can cd to another directory). You might do better with for dirs in */Seadont, too. Commented May 19, 2014 at 20:21
  • 1
    Thank you, I will make these changes. Commented May 19, 2014 at 20:24

1 Answer 1

1

Try this:

#!/bin/bash
#set -x

for dirs in * ;do
  echo $dirs
  if [ -d "Seadont"] ;then 
     sed -i.bak 's/USER1/USER2/g' $dirs 
   fi
done

Mind you that it won't work if there are white spaces on "$dirs". I can propose a better solution if it does, but I would need to know current directory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.