0

Hi guys i am new to this. Hoping that you could help me I am writing this (should be simple) bash script to check if the filetype is txt or zip. but it does not work

the code is:

#!/bin/sh
echo "please insert file"
read file
if [ $file == *.txt ]
then
emacs $file
elif [ $file == *.zip ]
then 
zip $file
fi
tobiasegli_te
  • 1,413
  • 1
  • 12
  • 18

2 Answers2

1

Use a case statement.

case "$file" in
*.txt) emacs $file ;;
*.zip) zip   $file ;;
esac
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
0

You can either use:

if [ ${file: -4} == ".txt" ]
and thus compare the last 4 characters

Or use:

if [[ $file == *.txt ]]
and thus use a regular expression (double brackets are available only in some shells but it is available in bash).

arne
  • 374
  • 1
  • 8