LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple question for i in `cat move.txt` (https://www.linuxquestions.org/questions/programming-9/simple-question-for-i-in-%60cat-move-txt%60-644177/)

Noto 05-22-2008 09:38 PM

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!

ntubski 05-22-2008 10:20 PM

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

konsolebox 05-22-2008 10:27 PM

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

konsolebox 05-22-2008 10:32 PM

Quote:

Originally Posted by ntubski (Post 3161985)
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?.. :)

Noto 05-23-2008 02:09 AM

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!

JoeyAdams 05-23-2008 10:24 AM

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 :)

Noto 05-23-2008 09:23 PM

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.

ntubski 05-23-2008 11:36 PM

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).

Noto 05-24-2008 02:17 AM

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
>

konsolebox 05-24-2008 06:26 AM

have you tried: ?
# sh move.txt
# sh < move.txt

and perhaps surrounding move.txt with quotes might also do the trick

ntubski 05-24-2008 02:36 PM

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!


Noto 05-24-2008 02:59 PM

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

ntubski 05-25-2008 04:52 PM

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.

David the H. 05-25-2008 06:32 PM

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.


All times are GMT -5. The time now is 01:02 AM.