LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2017, 12:49 PM   #1
Intern
LQ Newbie
 
Registered: Jul 2017
Posts: 1

Rep: Reputation: Disabled
syntax error near unexpected token `do' Linux RedHat


I am trying to change one or two portions of file names using this code. The problem is coming from line 4 and 6. When ran as is I get "syntax error near unexpected token `do'". But, if I open a terminal inside the folder I'm trying to work with (stressed) and lose the 'stressed/' the code works fine. I have tried running the dos2unix as well, but it did nothing. I want this in a shell, so it would be ideal if I could get this working from one file. This is my first time posting here, so thank you all for your help.

Also, if there is a command line to move the files back to where I go them after they have been edited, that would be awesome too.

mkdir stressed
mv *C2.tif stressed

stressed/for i in *.tif; do mv $i $(echo $i | sed 's/nd173_T/s01/g'); done

stressed/for file in s01*; do mv "$file" "${file/_XY1_C2/}"
done
 
Old 07-26-2017, 01:23 PM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 5,560
Blog Entries: 16

Rep: Reputation: 3300Reputation: 3300Reputation: 3300Reputation: 3300Reputation: 3300Reputation: 3300Reputation: 3300Reputation: 3300Reputation: 3300Reputation: 3300Reputation: 3300
Lines 3 & 4 look odd to me. Shouldn't it be:

for i in stressed/*.tif; do ........;done
for file in stressed/s01; do........;done
 
Old 07-26-2017, 01:29 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,353
Blog Entries: 13

Rep: Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411
Quote:
Originally Posted by Intern View Post
I am trying to change one or two portions of file names using this code. The problem is coming from line 4 and 6. When ran as is I get "syntax error near unexpected token `do'". But, if I open a terminal inside the folder I'm trying to work with (stressed) and lose the 'stressed/' the code works fine. I have tried running the dos2unix as well, but it did nothing. I want this in a shell, so it would be ideal if I could get this working from one file. This is my first time posting here, so thank you all for your help.

Also, if there is a command line to move the files back to where I go them after they have been edited, that would be awesome too.

mkdir stressed
mv *C2.tif stressed

stressed/for i in *.tif; do mv $i $(echo $i | sed 's/nd173_T/s01/g'); done

stressed/for file in s01*; do mv "$file" "${file/_XY1_C2/}"
done
Please post more relevant portions of your script, and use [code][/code] tags to encapsulate your code.

Also suggest if this is a bash script that you enable debug by adding "set -xv" before this section in the script to cause verbose debug output.
Quote:
Originally Posted by hazel View Post
Lines 3 & 4 look odd to me. Shouldn't it be:

for i in stressed/*.tif; do ........;done
for file in stressed/s01; do........;done
Not disagreeing with the assessment, however who knows if those are lines 3 & 4 of their script.
 
Old 07-26-2017, 04:33 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Intern View Post
I am trying to change one or two portions of file names using this code. The problem is coming from line 4 and 6. When ran as is I get "syntax error near unexpected token `do'". But, if I open a terminal inside the folder I'm trying to work with (stressed) and lose the 'stressed/' the code works fine. I have tried running the dos2unix as well, but it did nothing. I want this in a shell, so it would be ideal if I could get this working from one file. This is my first time posting here, so thank you all for your help.

Also, if there is a command line to move the files back to where I go them after they have been edited, that would be awesome too.

mkdir stressed
mv *C2.tif stressed

stressed/for i in *.tif; do mv $i $(echo $i | sed 's/nd173_T/s01/g'); done

stressed/for file in s01*; do mv "$file" "${file/_XY1_C2/}"
done
Code:
userx@devuan-do:~$ for i in Downloads/* do ; echo $i ; done
bash: syntax error near unexpected token `echo'

userx@devuan-do:~$ for i in Downloads/* ; do  echo $i ; done
Downloads/jwmmaker-master
Downloads/jwmmaker-master.zip
userx@devuan-do:~$
your semi colon is in the wrong place. simple as that.

as far as your code I cannot tell how you're actually getting the files from one place to another, or what you're actually trying to do. I got an idea but ...

Code:
mkdir stressed
mv *C2.tif stressed

stressed/for i in *.tif; do mv $i $(echo $i | sed 's/nd173_T/s01/g'); done

stressed/for file in s01*; do mv "$file" "${file/_XY1_C2/}"
done
1 make new dir
2 move file from old dir to new dir change name on move.
3 done
Code:
mkdir newDir

for i in OldDir/*.tif ; do mv -v $i ~/newDir/NewfileName ; done
working example, if you use cp (copy) you will keep originals and get new one with different name somewhere else.
Code:
$ mkdir NewDir

userx@devuan-do:~$ for i in Downloads/*.zip ; do  cp -v  $i ~/NewDir/GooBw.zip ; done
‘Downloads/jwmmaker-master.zip’ -> ‘/home/userx/NewDir/GooBw.zip’
$ ls NewDir
GooBw.zip
if more than one file , then they will over copy each other ending up with only one, being last moved. you need to create a way to vary the names on move. Using variables will work. string concatenation .. look it up

Last edited by BW-userx; 07-26-2017 at 04:54 PM.
 
Old 07-26-2017, 04:49 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,646
Blog Entries: 11

Rep: Reputation: 3674Reputation: 3674Reputation: 3674Reputation: 3674Reputation: 3674Reputation: 3674Reputation: 3674Reputation: 3674Reputation: 3674Reputation: 3674Reputation: 3674
Welcome to LQ!

As already pointed out, please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

From the posted example and description, hazel's comments look to be the most on target. Your syntax using the for construct as posted does not appear to be correct.

Code:
stressed/for i in ...
As for moving them back at a later time, simply another mv with appropriately adjusted source and target would be the simplest way, and you will already know how!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error near unexpected token Fixit7 Puppy 1 05-13-2014 03:28 AM
syntax error near unexpected token `}' Chris1969 Linux - Newbie 17 06-18-2013 01:54 PM
syntax error near unexpected token -what does this mean? rmnature Linux - Newbie 5 02-14-2009 08:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration