Hello,
I am running a script under ubuntu 16.04
I have no problem with the script. My question is general algorithm of for file command.
I just need to know how for file in *.txt process works.
Let's say, I wish to run the script by sorting filename:
or
or another way:
or
In summarize: If we do not indicate any criteria, what's the default working method of for cycle ?
Thank you
Boris
It's not the for cycle's feature but the way the shell expands the "glob"s using "pattern matching". man bash is your friend:
Quote:
Pathname Expansion
After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of filenames matching the pattern.
For any other arrangement of the value list, additional measures must be taken.
Why not check it by creating some files and doing for loop with a simple echo ?
Default order of things should be apparent from the output produced.
As for other requirements, it will have to be coded for each case specifically using if , case or both, piped to sort command or whatever.
Some like length of filename shell builtin can be used e.g. ${#i}
For sorting requirement per modified time a simple example for mtime change
Last modified file will be last printed on the screen.
Code assumes there are not comma , in filenames.
Hope that helps
Regards
Peasant.
These 2 Users Gave Thanks to Peasant For This Post:
Hi,
i would like to insert a if-then-else function in to cycle for
--------------
cat test
--------------
# cat test
ALFA
BETA
GAMMA
-----------------------
This is my script:
#!/bin/bash
for i in $(cat test); if ; then
echo "ok"
else (5 Replies)
i have a question how to modify below script to generate the expect result below :
test.sh
#!/bin/bash
for ((i=0; i < 25; i++))
do
echo $1$i
done
current result:
test.sh 20090101
200901010
200901011
200901012
200901013
200901014
200901015
200901016
200901017
200901018 (2 Replies)
Hello,
I have a question:
is there a way to have a "for" cycle done a certain number of times. For example in c++ I can do this:
for (i=o;i<10;i++)
and the cycle will be repeated 10 times.
in UNIX for example I do this:
for i in `cat /etc/host` do done
and the cycle will be repeated... (6 Replies)
hello everybody,
I need help on putting a wildcard match inside an if condition (I'm using korn shell):
if ]
then echo ' '
echo ''$MYSEL' is not a correct option'
echo ' '
else .....
i tried also #if -ne "``" and a lot of combinations of `"' but I didn't find the... (2 Replies)
Hi! I am new to HP-UX. :o
By using the command glance, I found the user memory usage was very high. I would like to know is there any command can show the process which consume most available memory ? (Just like the command top, but order by memory, not CPU) (1 Reply)
Hello,
I have files in a dir.
I what to create a FOR cycle that will do this
FOR <condition> do
file=`ls <directory> | tail -1`
echo $file
mv -f $file <another dir>
done
What I want to now is what should I put in the <condition>. The condition I want is that the FOR will execute... (3 Replies)
Hello
I got a cycle in the script which open another scripts.
if
then
action
fi
Scripts action will be running 2 times at the same time.
Inside of action() is insert into the table.
But what I want is that only first script can do insert into table.
So how to do... (2 Replies)