Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to write a shell script that will read the files in the directory and then i would need to each file (name) as the parameter to command (to run java class)
For(each file in the directory)
{
java testjava.java file_name
}
Not sure about the sub-directories... Try the -R flag for ls which is recursive. I'm not sure if it will output it in the correct format for bash to interpret.
To make "ls" work with spaces in filenames, just play around with the IFS variable.
By default, $IFS contains a space, a tab and a newline character, so output of ls is split on all those, including the spaces in the names of files.
Setting $IFS to only include the newline will avoid this.
You can restore $IFS to it's normal setting afterwards too.
Code:
backup=${IFS}
IFS="\n"
for i `ls`; do
...
done
IFS=${backup}
or something like that...
But in this case, the "find" command is indeed a better solution.
I never knew that variable existed timmeke - cheers
BUT... :P
Setting it to only a new line would mean you'd have to alter the output of ls with the -l option wouldn't it? and then you would get all the other output (permissions, dates, size etc) that goes with the -l option...
Setting it to only a new line would mean you'd have to alter the output of ls with the -l option wouldn't it?
The default ls command only produces output that goes across the line when it knows it's writing to a tty device (a terminal of some sort). When it's being used in scripts, with its output being used as here, it outputs in a column.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.