this post was submitted on 07 Jun 2024
4 points (62.5% liked)

#!/bin/bash

77 readers
1 users here now

A community to discuss about Bash.

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. The shell's name is an acronym for Bourne-Again SHell, a pun on the name of the Bourne shell that it replaces and the notion of being "born again".

Keep discussion limited to Bash and projects around it. Feel free to share resources, self-projects, tutorials or any other cool stuff related to Bash.

Icon Attribution : Free Software Foundation, FAL, via Wikimedia Commons.

founded 10 months ago
MODERATORS
 

Seriously. There doesn't seem to be a way to do this. Every thing I ever try I just get bad substitution errors. The internet is full of people posting code that's supposed to compare file extensions but none of it works. I've spent all morning trying everything I could find. I already gave up and I'm making this progeam in python instead but now I'm curious. How tf do you actually compare file extensions? If I have a folder fill of files and I want to run a command only on the png files, there seems to be no way to actually do this.

If someone posts "[[ $file == *.txt ]]" I'm going to fucking scream because THAT DOES NOT WORK. IT'S NOT VAILD BASH CODE.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 3 points 6 months ago* (last edited 6 months ago) (1 children)

for i in ``ls *.png``; do something $i; done;

(not formatting correctly)

Is this sort of what you mean? You can tune the loop, but essentially you build a list of the files you want to do something against, then loop through it.

[–] [email protected] 5 points 6 months ago* (last edited 6 months ago) (1 children)

What's the benefit of spawning a subshell and executing "ls" here instead of just passing a glob to your loop?

$ for lol in /usr/share/*.lm;do printf "I found a file named '%s'\n" "$lol";done

I found a file named '/usr/share/out-go.lm'
I found a file named '/usr/share/ragel.lm'
I found a file named '/usr/share/ril.lm'
I found a file named '/usr/share/rlhc-c.lm'
I found a file named '/usr/share/rlhc-crack.lm'
I found a file named '/usr/share/rlhc-csharp.lm'
I found a file named '/usr/share/rlhc-d.lm'
I found a file named '/usr/share/rlhc-go.lm'
I found a file named '/usr/share/rlhc-java.lm'
I found a file named '/usr/share/rlhc-js.lm'
I found a file named '/usr/share/rlhc-julia.lm'
I found a file named '/usr/share/rlhc-main.lm'
I found a file named '/usr/share/rlhc-ocaml.lm'
I found a file named '/usr/share/rlhc-ruby.lm'
I found a file named '/usr/share/rlhc-rust.lm'
[–] [email protected] 7 points 6 months ago

The benefit is I get taught something new!