Question

Shell script Write a case condition that: Store the file name by default in $FILE or...

Shell script

Write a case condition that:

Store the file name by default in $FILE or you may read it as input argument while running the script.

Case 1- checks if file is a directory

Case 3- file is a regular file

Case 2- if a file is readable

Case 3- if file is writeable

Problem 5: Write a shell script that reads a filename; and verifies the file extension to be text/ jpeg/gif/png. If it is text file also display the contents of the file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

if [ -z "$1" ]
then
   file="/home/madhav/Desktop/HomeworkLib/test"
else
   file=$1
fi


case "$(ls -ld -- $file)" in
-*) echo 'Regular file' ;;
d*) echo 'Directory' ;;
r*) echo 'Readable' ;;
w*) echo 'Writeable' ;;
*) echo 'other' ;;
esac

#!/bin/bash
file=$1

filename=$(basename -- "$file")
extension="${filename##*.}"
filename="${filename%.*}"

echo "given argument's filename is: $filename"
echo "given argument's extension is: $extension"

cat $file

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)

Add a comment
Know the answer?
Add Answer to:
Shell script Write a case condition that: Store the file name by default in $FILE or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT