-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.sh
54 lines (46 loc) · 964 Bytes
/
Extension.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/bash
echo -e "Enter the file name : \c"
read fileName
if [ -z $fileName ]
then
echo -e "Please write file name : \c"
read fileName
else
if [ -f $fileName ]
then
echo -e "Enter the extension : \c"
read extension
if [ -z $extension ]
then
echo "Please write extension!"
exit 1
fi
#use to get the filename without extension
file_without_extension=${fileName%.*}
#use to get the extension without name
file_extension=${fileName##*.}
if [ $file_extension = $extension ]
then
echo "File has already extension"
exit 1
else
newFile="$file_without_extension.$extension"
mv $fileName $newFile
echo "The file $fileName renamed to $newFile"
fi
else
echo -e "File is not found.Do you want to create a new file with name(y/n) : \c"
read choice
if [ $choice = "y" -a "Y" ]
then
touch $fileName
echo -e "Enter the extension : \c"
read extension
newFile="$fileName.$extension"
mv "$fileName" "$newFile"
echo "New file $newFile created"
else
exit 1
fi
fi
fi