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! |
You could change IFS to avoid the splitting
Code:
# by changing IFS Code:
while read line ; do Code:
. move.txt |
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 |
Quote:
|
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! |
Quote:
Also, I recommend using echo to see what is being done before running loops like these. Thank you for reading my useless reply :) |
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. |
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 |
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 > |
have you tried: ?
# sh move.txt # sh < move.txt and perhaps surrounding move.txt with quotes might also do the trick |
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 |
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 |
Quote:
You can use dos2unix instead of vi. Or use a different editor on Windows that can save with unix line endings. |
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. |