You are not logged in.

#1 2020-07-08 20:57:09

OrigBitmancer
Member
Registered: 2019-01-22
Posts: 144

[SOLVED] Bash script character range loop syntax

I cannot remember my basic bash. Please help.

Given a set of files, file_#.text, where # is selected from [ABCDEFGHJKLMNPQ], a closed set of known letters,
how do I write a loop executing once per letter?

I would check my loop by the statement

  echo process1 file_#.text otherfile_#.text

to make sure the substitution is correct,
(where what will execute when finished is "process1 file_#.text otherfile_#.text").

Last edited by OrigBitmancer (2020-07-09 01:56:08)

Offline

#2 2020-07-08 21:14:12

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] Bash script character range loop syntax

for x in *.text; do the_thing "$x"; done

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2020-07-08 21:44:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: [SOLVED] Bash script character range loop syntax

JWR, the simple for loop would not acheive the main goal of having that letter in both arguments to the process.

The direct answer to your question is this:

for N in A B C D E F G H J K L M P Q; do
   process1 file_${N}.text otherfile_${N}.text
done

But if you are not exactly sure what the letters are, or if they may change, the following more flexible approach might be better:

for fname in file_*.text; do
   process1 $fname otherfile_${fname#file_}
done

EDIT: looking back on this, the command in my second loop is pretty silly, this would have the same result:

   process1 $fname other${fname}

Last edited by Trilby (2020-07-09 02:12:50)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2020-07-09 01:55:44

OrigBitmancer
Member
Registered: 2019-01-22
Posts: 144

Re: [SOLVED] Bash script character range loop syntax

Trilby wrote:

JWR, the simple for loop would not acheive the main goal of having that letter in both arguments to the process.

The direct answer to your question is this:

for N in A B C D E F G H J K L M P Q; do
   process1 file_${N}.text otherfile_${N}.text
done

But if you are not exactly sure what the letters are, or if they may change, the following more flexible approach might be better:

for fname in file_*.text; do
   process1 $fname otherfile_${fname#file_}
done

Thank you very much! Your first solution did the trick. It was a mistake in my punctuation in my source.

Not quite sure why the other reply was unhelpful?

Last edited by OrigBitmancer (2020-07-09 01:56:41)

Offline

#5 2020-07-09 02:08:47

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] Bash script character range loop syntax

OrigBitmancer wrote:

Not quite sure why the other reply was unhelpful?

I had misunderstood your requirements; I thought you just wanted to loop over a set of files.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB