LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-22-2008, 09:38 PM   #1
Noto
LQ Newbie
 
Registered: May 2008
Posts: 5

Rep: Reputation: 0
Simple question for i in `cat move.txt`


I'm having a bit of a problem with a shell script im trying to write.
I have a file called move.txt in it there are lines like this
cp file destination
cp file destination
cp file destination
cp file destination
And so on.

When I run it I get some problems.
for i in `cat move.txt`;do $i;done
: command not found
cp: missing file operand
Try `cp --help' for more information.
-bash: move.txt: command not found
: command not found
: command not found


So I do this to see whats going on and I get this.
for i in `cat move.txt`;do echo $i;done
cp
move.txt
move55.txt

Its splitting the line of cp move.txt move55.txt in move.txt at every space... How do I fix thi?

Thanks!
 
Old 05-22-2008, 10:20 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,440

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
You could change IFS to avoid the splitting
Code:
# by changing IFS

OLD_IFS=$IFS
IFS='
'
for i in `cat move.txt`; do
    echo $i
done

# restore IFS to avoid weirdness later on
IFS=$OLD_IFS
but I prefer
Code:
while read line ; do
    echo $line
done < move.txt
EDIT: actually if all the lines in move.txt are of that form, then it's really a shell script so you could just do
Code:
 . move.txt

Last edited by ntubski; 05-22-2008 at 10:22 PM.
 
Old 05-22-2008, 10:27 PM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
it seems that you made your own work so i'll be glad to answer your question..

insert IFS=$'\n' before the for statement or use

Code:
while read LINE; do
   eval "${LINE}"
done < move.txt
# or i don't know if this will work:
while read -a LINEA; do
   ${LINEA[@]}
done < move.txt
# if not a similar eval should do the trick.. or if it's really just 3 fields, just do 
while read A B C; do
   ${A} ${B} ${C}
done < move.txt
edit: sorry for the doubled answers .. ntubski's reply came when i was still making mine

Last edited by konsolebox; 05-22-2008 at 10:29 PM.
 
Old 05-22-2008, 10:32 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by ntubski View Post
EDIT: actually if all the lines in move.txt are of that form, then it's really a shell script so you could just do
Code:
 . move.txt
why is it tha we always tend to solve problems the complicated way?..
 
Old 05-23-2008, 02:09 AM   #5
Noto
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks guys for the quick reply... lol can't beleive I have been banging my head into the wall when all I had to do was execute the file...

You guys rock!
 
Old 05-23-2008, 10:24 AM   #6
JoeyAdams
Member
 
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
Quote:
for i in `cat move.txt`;do $i;done
You can use $() instead of ``, the latter of which has syntactical problems when you try to nest.
Also, I recommend using echo to see what is being done before running loops like these.

Thank you for reading my useless reply
 
Old 05-23-2008, 09:23 PM   #7
Noto
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Ok I admit im really new to this but I need to execute move.txt from another Shell script.
If I run move.txt like ./move.txt or . move.txt it works fine but when I run it though a shell script in the same folder using the same command it gives me :No such file or directorytxt

move.txt
echo test!

test.sh
. move.txt


I will be adding more things to the test.sh file that need to be done just want to get this basic part down.
 
Old 05-23-2008, 11:36 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,440

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
The shell will look for move.txt in the current directory, so if you don't execute the script from the directory with move.txt then it won't be found. You can put the full path to move.txt in the script or use $(dirname $0) to get the directory of the script being executed.

test.sh
Code:
#!/bin/bash

# assuming move.txt is in the same directory as test.sh
. $(dirname $0)/move.txt
Note that this doesn't work if you source test.sh (then $(dirname $0) gives the directory containing the bash binary).
 
Old 05-24-2008, 02:17 AM   #9
Noto
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Hmm its still doing the same thing.
I did have it in the same folder but added your code too and its doing the same thing kinda... Here are the results of me cating the 2 files so you can see them and then executing the .sh

Here is before what I had
>cat move.txt
echo TEST!

>cat test.sh
. $(dirname $0)/move.txt

>./test.sh
: No such file or directorytxt
>

After the change
>cat move.txt
echo TEST!

>cat test.sh
. $(dirname $0)/move.txt

>./test.sh
: No such file or directorye.txt
>
 
Old 05-24-2008, 06:26 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
have you tried: ?
# sh move.txt
# sh < move.txt

and perhaps surrounding move.txt with quotes might also do the trick
 
Old 05-24-2008, 02:36 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,440

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
Those error messages look kind of weird, maybe you have some control characters in your files and stuff is getting overwritten on output? Did you create these files on Windows? I can't tell what's wrong, since this works for me:
Code:
~/tmp$ cat move.sh
#!/bin/bash

. $(dirname $0)/move.txt
~/tmp$ cat move.txt
echo TEST!
~/tmp$ ./move.sh
TEST!
~/tmp$ cd ..
~$ tmp/move.sh
TEST!
 
Old 05-24-2008, 02:59 PM   #12
Noto
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Ok thanks again. I'm not a big fan of VI since I hadn't used it in some time... But yea it was because I was editing the file on a windows system and uploading it... VI :wq seemed to fix it for me tho...

Sorry for being such a noob and thanks a lot for all your help

Daniel
 
Old 05-25-2008, 04:52 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,440

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
Quote:
Sorry for being such a noob
No need to apologize for that

You can use dos2unix instead of vi. Or use a different editor on Windows that can save with unix line endings.
 
Old 05-25-2008, 06:32 PM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
If you run 'file move.txt', it will show you what kind of file it is. For text files, it usually shows you the encoding, and the kind of line endings it uses if other than unix. If it says it has CRLF line terminators, it's using dos line endings.

There are several applications that can convert between dos and unix line endings, such as flip. Many GUI text editors such as kwrite will also allow you to change them.
 
  


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
shell script to move files from one system to another sytem with file names in a txt coral_km Linux - Newbie 3 02-13-2008 10:23 PM
why openoffice (spreadsheet) has no save as simple TXT ? Xeratul Linux - Software 5 05-29-2007 05:34 PM
LXer: Task management made simple with todo.txt and Tux ToDo LXer Syndicated Linux News 0 10-05-2006 10:21 PM
simple question - can you move header and lib files from one box to ano dewcansam Linux - General 3 11-25-2001 12:35 AM
simple question - can you move header and lib files from one box to another without a dewcansam Linux - Newbie 3 11-06-2001 05:35 PM


All times are GMT -5. The time now is 10:21 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration