Error: syntax error near unexpected token

BenTom07

New Member
Joined
Dec 7, 2022
Messages
7
Reaction score
0
Credits
128
I'm trying to write a shell script to automate some tasks on my Linux server, but I'm getting a syntax error when I try to run it. Here is the code:
Code:
#!/bin/bash

for file in /path/to/dir/*
do
  if [[ "$file" =~ \.txt$ ]]
  then
    sed -i 's/old text/new text/g' $file
  fi
done
When I run the script, I get the following error: 'syntax error near unexpected token `then' on line 6. Can anyone help me understand what's causing this error and how I can fix it?"
 


I'm trying to write a shell script to automate some tasks on my Linux server, but I'm getting a syntax error when I try to run it. Here is the code:
Code:
#!/bin/bash

for file in /path/to/dir/*
do
  if [[ "$file" =~ \.txt$ ]]
  then
    sed -i 's/old text/new text/g' $file
  fi
done
When I run the script, I get the following error: 'syntax error near unexpected token `then' on line 6. Can anyone help me understand what's causing this error and how I can fix it?"
That script looks absolutely fine to me. That should just work.
Are you sure you don't have any weird characters between the end of line 5 and the start of line 6?

I've just tested your code in a sub-directory of my linux.org directory, where I put any programs/scripts that I've worked on for people in threads here at linux.org. I add a directory for each user - that way I can associate a script with the user who asked a particular programming/scripting question.

To test your script - this is the actual code I used.
Bash:
#!/bin/bash

for file in ~/Projects/linux.org/BenTom07/*
do
  if [[ "$file" =~ \.txt$ ]]
  then
    sed -i 's/old text/new text/g' "$file"
  fi
done

I created three .txt files in the BenTom07/ directory which were just filled with lines containing "old text". I ran your script and as expected - all three of the .txt files were populated with "new text" instead.

So I have no idea why you're getting that error. Your code works properly for me!

The only things I changed in the script were the path/to/dir, to ensure it used a valid path that exists on my system. Also in the sed invocation, I have put double quotes around "$file" to ensure that the filename is properly quoted. That will avoid problems if any of the .txt files contain names with spaces or other special characters. Spaces and special characters in file-names either need to be double quoted, or escaped. So in a script, it makes sense to use double-quotes when de-referencing strings from variables. Especially if they contain file-names. But that change will have had nothing to do with the error you were getting.

So I'm mystified. The code you've posted is absolutely fine. I dont' know how/why you're getting that error.

The only thing I can think of is, perhaps your script has some kind of funky character somewhere between line 5 and line 6? Or between line 6 and line 7.


But everything you've posted is OK!
 
That script looks absolutely fine to me. That should just work.
Are you sure you don't have any weird characters between the end of line 5 and the start of line 6?

I've just tested your code in a sub-directory of my linux.org directory, where I put any programs/scripts that I've worked on for people in threads here at linux.org. I add a directory for each user - that way I can associate a script with the user who asked a particular programming/scripting question.

To test your script - this is the actual code I used.
Bash:
#!/bin/bash

for file in ~/Projects/linux.org/BenTom07/*
do
  if [[ "$file" =~ \.txt$ ]]
  then
    sed -i 's/old text/new text/g' "$file"
  fi
done

I created three .txt files in the BenTom07/ directory which were just filled with lines containing "old text". I ran your script and as expected - all three of the .txt files were populated with "new text" instead.

So I have no idea why you're getting that error. Your code works properly for me!

The only things I changed in the script were the path/to/dir, to ensure it used a valid path that exists on my system. Also in the sed invocation, I have put double quotes around "$file" to ensure that the filename is properly quoted. That will avoid problems if any of the .txt files contain names with spaces or other special characters. Spaces and special characters in file-names either need to be double quoted, or escaped. So in a script, it makes sense to use double-quotes when de-referencing strings from variables. Especially if they contain file-names. But that change will have had nothing to do with the error you were getting.

So I'm mystified. The code you've posted is absolutely fine. I dont' know how/why you're getting that error.

The only thing I can think of is, perhaps your script has some kind of funky character somewhere between line 5 and line 6? Or between line 6 and line 7.


But everything you've posted is OK!
No, I don't have any characters between any lines. Also, I have no idea why I'm getting that error. Idk what to do.
 
Retype the script......not copy and paste....retype from @JasKinasis's post
 
Last edited:
Moved to Command Line, where scripting inquiries are handled.

Wizard
 

Members online


Top