Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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 have been trying to come up with a program that can do this:
Say I have a file named "sire" with a single field;
10
229
288
35
83
47
3
I want to create a file "gen" with three fields with the data in file "sire" listed in field 1 while field 2 and 3 are just 1 each like this:
10 1 1
229 1 1
288 1 1
etc
I want to use this to solve an equation of the form "itgen -m 500 -r gen b beta -n inverseNRM
-m 500 indicates the number of rounds to iterate
-r gen is the right hand side of the equation
-b beta is the filename for the solution
-n inverseNRM is another file with three fields.
I never intended to be rude. I was told the title of my first post was inappropriate and I was to use something like urgent and name of program which I just did.
Its important to me, sorry if I offended you
the sed gave me a file with three fields which is a step. I am actually trying to run a while loop such that it reads every line and it uses each line to do a command ("itgen -m 500 -r gen b beta -n inverseNRM) which I saved in a file called perm. It should output each result for each line after doing the command. I tried using something like this:
IFS=$'\n';for line in `cat gen`; do ./perm ${line} > ${line}; done.
When I did this, it said permission denied, although it gave me each line of gen, there was no content.
I am a little new in programing, I will appreciate some help thank you
I must say I don't quite understand how the initial request and the itgen -m 500 -r gen b beta -n inverseNRM
hang together ... irrespective of the content of the line you wish
to run the same command over and over?
\
How does the BOLD bit above hang together w/ ./perm?
Can you give us a fleshed out example of input, and the desired
effect, plus the expected output?
I used IFS=$'\n';for line in `cat gen`; do ./perm ${line} > ${line}; done on the command prompt. I saved itgen -m 500 -r gen b beta -n inverseNRM in a file called perm. The content of perm is an equation I want to solve. Its like gen X beta= inverseNRM, then beta= gen-1 X inverseNRM. But that is a different program on my computer. inverseNRM is another file
how to read each file in a directory and pass each file separately into a do command
Code:
I have been trying to read files in a directory then pass it to a command so that each file gets processed in the do command and the output after the command gets stored separately file by file.
I have this directory:
Code:
home/pro/connection
The files in the directory connection are names:
S1:
Code:
21 3 2
S2
Code:
20 1 2
Code:
I want to loop through each file and send each file to be processed in the do command, then the result of each file processed gets saved differently and each gets moved into one folder.
Something like this:
Code:
#!/bin/bash
i=1
FILES=/home/pro/connection/*
for f in $FILES
do
eacho "Processing of $f file...:# take action on each file. $f store current file name
cat $f | command # output from this command for S1 is beta >> beta >> beta.$i # so file S1 and S2 have outputs beta1 and beta2
done
Code:
can I use while command for this as well?
Code:
Is it possible to create S1 and S2 without having to output them just create them and send them separately to the do command? The final output (beta1 and beta2) are more important than outputting files S1 and S2 (though I need them to get sent to the do command)
Distribution: Red Hat, CentOS, Scientific, Fedora, Ubuntu, SUSE, SLES
Posts: 14
Rep:
Linux, Bash Script
You can certainly use while, however, for .. do .. done is, IMHO, a better approach. So your code should look something like
#/bin/bash
DIRECTORY=/home/pro/connection
OUTPUT_FILE=$DIRECTORY/out.txt
#Please choose a command
#COMMAND=something
COMMAND='ls -l'
for f in $DIRECTORY/*
do
echo "Processing file $f"
#Applying this command onto each file separately
#And redirecting (appending) to an output file
$COMMAND $f >> $OUTPUT_FILE
done
If you want to create files without redirection, you can simply use the touch command
touch afile bfile
For details of this command you can check the man pages
man touch
Kindly elaborate if you have any more questions and I would be glad to assist.
Regards,
Farzan Mufti
Last edited by Farzan Mufti; 07-13-2012 at 12:17 PM.
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.
And iconig, please use them only for code and data. Don't put them around the explanation text. I can hardly read your post.
Distribution: Red Hat, CentOS, Scientific, Fedora, Ubuntu, SUSE, SLES
Posts: 14
Rep:
Bash scripting
Well,
In advanced shell scripting variables ARE used to store commands also. It makes the code a lot more readable.
There is simply nothing wrong with
Code:
FILES=/home/pro/connection/*
It is very common in Bash scripting to do this and has no problem with it. There is no "no no" to it, period. It is not wrong because first, it runs in Bash and second, it is not a bad practice. Bash for statement works fine with it and making it an array is needless.
for f in $DIRECTORY/*
would be good.
If you want to get file list of the current directory, you can do like
Code:
for f in *
do
Last edited by Farzan Mufti; 07-13-2012 at 12:06 PM.
I completely disagree with you, and so do all the very smart people at the wooledge site and others. You should always use the right tool for the right job.
Contrary to your assertion, putting the command in a variable obscures what's being run. It blurs the line between data and code. Not to mention that the shell parsing rules make it tricky-to-impossible to handle special characters like spaces inside them. Again, variables are for data (or at most arguments to commands), and functions are for code.
As for arrays, a scalar variable holds a single value, and you'd have to rely on shell word-splitting or other unreliable splitting techniques to extract multiple entries from them. But arrays are specifically designed for storing and operating on multiple entries of related values. They are particularly suited for working with lists of filenames, in cases where you can't just loop over them directly.
Actually, I do have to apologize in regards to my initial criticism. globs don't expand in "var=value" lines, so actually this is acceptable:
Code:
FILES=/home/pro/connection/*
for f in $FILES
"$FILES" expands into the globbing pattern, and the pattern then expands into a list of files that the for loop can safely accept. The variable never actually contains the file list itself.
But as for "for f in $(ls directory)", I say it again, and even louder...NO, NO, NO, NO, NO! The for loop is simply not designed for processing the output from commands like ls or cat. Read the link I gave carefully and please do not ever suggest it again. I'm serious. The only time it is acceptable is when you use a command that outputs discreet values that are not affected by word-splitting and globbing expansion, such as seq.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.