Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 699  / 3 Years ago, sun, august 1, 2021, 5:22:35

I have found that this command gives me the mime type from a file:


file --mime-type dog.jpeg 

Output:


dog.jpeg: image/jpeg

I am now trying to create a bash checks if the mime is jpeg og png. However I am a bit stuck:


#!/bin/bash
$file_mime="file --mime-type dog.jpeg"
mime=`"${file_mime#*:}"`

if(mime=='image/jpg' OR mime=='image/png') do:
echo"Jpg or png"
done

Output:


./bash.sh: line 2: =file --mime-type dog.jpeg: command not found
./bash.sh: line 3: : command not found

More From » bash

 Answers
5

Your first error is because you have a $ prefix on your assignment. Your second error is because you are trying to combine the execution of the command with the processing of the results (stripping the filename from the result). As always with bash scripting there are lots of ways to achieve the same thing so this is just one, try:


#!/bin/bash

mime=$(file -b --mime-type dog.jpeg)

if [[ $mime = image/@(jpeg|png) ]]; then
echo "File is a jpeg or png."
fi

Then evaluate $mime but I suggest you search and read up on some bash scripting tutorials for if statements etc as your if statement is not valid bash script.


[#1937] Tuesday, August 3, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
learty

Total Points: 432
Total Questions: 115
Total Answers: 109

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
learty questions
Sat, May 21, 22, 15:30, 2 Years ago
Sun, Jan 9, 22, 22:31, 2 Years ago
Mon, Jul 4, 22, 11:31, 2 Years ago
Tue, Mar 22, 22, 16:28, 2 Years ago
Thu, Feb 10, 22, 02:33, 2 Years ago
;