Bash While Loop is a loop statement used to execute a block of statements repeatedly based on the boolean result of an expression, for as long as the expression evaluates to TRUE. Syntax of Bash While Loop.
I require to compose a screenplay that starts my plan with various arguments, but I'michael fresh to Party. I begin my system with:
./MyProgram.exe Information/data1.txt Logs/data1Log.txt
.Here is definitely the pseudocode for what I wish to do:
So I'm actually confused how to generate second case from the very first one, so it appears like dataABCDLog1.txt and start my program.
DobrobobrDobrobobr2,38533 platinum badges1010 gold badges1414 bronze badges
5 Solutions
A couple of information first: when you make use of
Information/data1.txt
as an discussion, should it really become/Data/data1.txt
(with a leading slash)? Furthermore, should the external loop check just for.txt documents, or all documents in /Data? Here's an answer, presuming/Information/data1.txt
and.txt files just:Information:
/Data/.txt
expands to the paths of the text documents in /Data (likethe /Data/ component)$(. )
operates a shell command and inserts its output at that stage in the command word seriesbasename somepath.txt
outputs the base component of somepath, with.txt eliminated from the end (elizabeth.gary the gadget guy./Data/file.txt
-gt;file
)
If you needed to run MyProgram with
Alan Watts. SmithInformation/file.txt
rather of/Information/file.txt
, make use of'$filename#/'
to eliminate the top cut. On the some other hands, if it's reallyInformation
not/Data
you desire to check, just make use offor filename in Information/.txt
.17.8k33 money badges5151 metallic badges7878 bronze badges
Gordon DavissonGordon Davisson73.8k1111 magic badges8080 sterling silver badges9696 bronze badges
Sorry for necromancing the thread, but whenever you iterate over data files by globbing, it's great practice to prevent the corner case where the glob does not go with (which makes the loop adjustable expand to the (un-matching) glob pattern line itself).
For instance:
Guide: Party Risks
Cong MaCong Mother5,80122 gold badges2020 silver badges4343 bronze badges
The
name=$file##./
substitution (shell parameter development) removes the leading pathname up to the final/
.The
Jonathan LefflerJonathan Lefflerbase=$name%.txt
replacement eliminates the trailing.txt
. It'beds a little bit more difficult if the extensions can vary.583k9696 gold badges704704 magic badges10541054 bronze badges
You can use finds null divided output option with read to iterate over directory structures securely.
Therefore for your case
additionally
will run the while loop in the present scope of the screenplay ( procedure ) and allow the result of find to be used in setting up factors if needed
ArcabardArcabard9,2861313 yellow metal badges4242 magic badges6666 bronze badges
Appears like you're also trying to carry out a home windows file (.exe) Surely you ought to be using powershell. In any case on a Linux bash cover a basic one-liner will be sufficient.
Or in a bash
consumer3183111user3183111
Not the response you're searching for? Browse other queries marked bashfor-loopfilenames or talk to your very own query.
I have the adhering to:
Which will go into each index in my route, installments
a
and printsa
. Nevertheless, the output is generally 1. Why is usually that?NoblenectroNoblenectro
2 Solutions
From bash(1):
By merely removing the parenthesis you have around that wedge of code, you would have something like this:
(furthermore slightly even more conventionally formatted)
the outcome is certainly:
Låkan LindqvistHåkan Lindqvist
Because you place the loop body in unnecessary 's i9000 which can make it perform in a subshell if I remember correctly.
SpiffSpiff79.1k1010 silver badges121121 magic badges166166 bronze badges