PDA

View Full Version : Basic redirection problem


nealbo
11-06-2006, 06:38 AM
Hi, I'm trying to write a simple script that writes the output of ls to a file. I'm using:

#!/bin/sh
$ls >> text.txt

and I've tried

#!/bin/sh
$ls > text.txt

Both of these create the file text.txt but don't add ls into it. There are files in the directory, so thats not the problem.
When I echo ls:

echo "$ls"

I get a blank line echoed.

What am I doing wrong?

And I also tried having a space in between $ and ls - because I've seen it written that way on some tutorial websites, but that gives me an error.

nox-Hand
11-06-2006, 04:13 PM
Ah, I usually am in Linux, but due to Flash Webdesign, I had to dust off the old Windows HDD.. Lemme see here...

Do you want the contents of the file into the other file, or the list of things in the folder?


If you want to have the contents of the folder, shouldn't you do:

ls >> text.txt

I don't see where the $ is needed, but that's probably just me...

If you want the file into another .txt, you must do:

less <file> >> text.txt


the $ is in some systems but a symbol for the start of the command line. Like # is. Try without it :)

corey84
11-06-2006, 04:20 PM
you arent actually writing anything from the shell to the file because your variable has no data try this


#!/bin/sh
echo 'This is my data to my text file' > test.txt



System variables in a linux shell are all uppercase so if you are getting this variable from the system it needs to be $LS, also >> appends to the file and > writes to the file

nealbo
11-08-2006, 06:47 AM
Thanks for the replies - I've got it working now.
I didn't need the $