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.

6 points

File extensions are just a text based convention. Renaming a file to end in .PDF does not make the file a valid PDF.

You’re really saying there’s no way for a posix shell to take the text to the right of the last “.” character and then do simple string comparison on the result?

Also why can’t you just use normal globbing to feed arguments to your command? Why do you need to involve flow control?

permalink
report
reply
6 points
*

That works in bash 3.2 for me. So does this:

[[ $file = *.txt ]]

Maybe, the error is somewhere else in your code.

permalink
report
reply
4 points
6 points

I loved this comment:

What is the extension of the file xyzzy.tar.gz? Or plugh.cfg.saved? In other words, are you treating extension as a simple technical issue or a semantic one?

For an OS that does not have file “extensions”, it’s entirely up to the user to define whatever it is that they are trying to accomplish.

permalink
report
parent
reply
3 points

Are you actually using Bash? You might be in a different shell. You can check with echo $SHELL

permalink
report
reply
3 points
*

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.

permalink
report
reply
5 points
*

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'
permalink
report
parent
reply
7 points

The benefit is I get taught something new!

permalink
report
parent
reply

#!/bin/bash

!bash@programming.dev

Create post

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.

Community stats

  • 1

    Monthly active users

  • 4

    Posts

  • 24

    Comments

Community moderators