Program for changing access rights and register of file / directory names on Bash

As part of the laboratory exercises, we needed to write a script that, when entering the appropriate key, had to change the case of letters in the file / directory name from upper to lower and vice versa, change the case of the first letter of each word in the name to upper, with separate keys, which should not be executed independently, do the same conversions, but with the output of information to the terminal and perform register changes recursively.

With the teacher’s footsteps, our script has acquired additional features. So in our script, first, checks for the presence of forbidden characters in the file names and Cyrillic alphabet appeared, a check for write permissions (we decided to limit ourselves to files with write permissions):

The code
if [[ -w \"$n\" ]]; then
if [[ "$adress" != *[--]* ]] && [[ "$adress" != *[\"\`\'\:\?\<\>\|\!]* ]]; then

After adding various checks and providing for various kinds of errors, the question arose, what will happen if the system cannot reproduce the Russian language? The question was the place to be, because All information issued by the user was written in Russian. For a while we ignored this question, the teacher didn’t remember about it, okay, but we redid our script and created a graphical interface for it using Zenity.
For convenience, we split the script into several files, in the initial file we set the variables, which are then passed on, and we check for Zenity.

The code
pr_way=`pwd`
pr_way="${pr_way#*$USER/}"
pr_name="$0"
pr_name="${pr_name#*/}"
start_scr="0"
#----------------------------------------------------------------
if find /usr/bin/zenity
then
cd selection
./language.sh "$pr_way" "$pr_name" "$start_scr"
else
echo "Zenity is not installed on this computer."
fi


Having made a fully graphical interface, we returned to the question of language. They limited themselves to three languages: Russian, English and Belarusian. The language is automatically selected, depending on your system language, if it does not turn out to be one of these three, then a language selection window will simply open where you can choose it yourself.

The code
if [[ $LANG == *["ru"]* ]]; then
	./key-ru.sh "$pr_way" "$pr_name" "$start_scr"
elif [[ $LANG == *["en"]* ]]; then
	./key-en.sh "$pr_way" "$pr_name" "$start_scr"
elif [[ $LANG == *["by"]* ]]; then
	./key-by.sh "$pr_way" "$pr_name" "$start_scr"
elif [[ $LANG != *["ru"]* ]] && [[ $LANG == *["en"]* ]] && [[ $LANG == *["by"]* ]]; then
language=$(zenity \
	--list --width=400 --height=150 \
	--title="Language selection" \
	--text="Sorry. This program does not support your system language. Please, select language below." \
	--column="Language" \
	"Russian" \
	"English" \
	"Belarussian" )


image

After determining the language in which you will continue to work, a window opens with a choice of key.

image

Once you have decided what you want to do by selecting the key, proceed to the selection of the file / directory with which you will do this.

image

After completing the work, the program will inform you of what happened and will kindly ask if you want to continue.

image

As a bonus, we added to our program the ability to change the access rights of files / directories, this was our previous task, therefore, it was decided to combine them.

The code

#!/bin/bash
#    
adress="$1"
pr_way="$2"
pr_name="$3"
echo 
cd
#     "/",  ,   /   - 
if [[ "$adress" == *["/"]* ]]; then
#    /  
way="${adress%/*}"
name="${adress##*/}"
cd "$way"
else
#   "/"  ,  /  
name="$adress"
fi
#     
format=$(zenity \
	--list --width=400 --height=150 \
	--title="   " \
	--text=", ,    ." \
	--column="" \
	"" \
	"" )
#    
if [ $? -eq "1" ];then
	exit
fi
case "$format" in
	)
#      
mod=$(zenity \
	--entry \
	--title="  " \
	--text=", , ,     '$name'.
     ,      0  7:
0 –   ;
1 –   ;
2 –   ;
3 –    ;
4 –   ;
5 –    ;
6 –    ;
7 –  ." \
	--entry-text="")
#    
if [ $? -eq "1" ];then
	exit
fi	
#    (3  ,     0  7)
if [[ $mod == *[0-7]* ]] && [[ ${#mod} = 3 ]]; then
#  
	chmod $mod "$name"
#  
	zenity --info --title=" " --text="'$name'    '$mod'."
else
	zenity --error --title="!" --text="    . ,         ."
fi
;;
	)
#     
mod=$(zenity \
	--entry --width=600 \
	--title="  " \
	--text="  ,     '$name'.
     ,      (u/g/o/a),
,    (+/-/=)      (r/w/x):
u – ;
g – ;
o – ;
a – ;
+ – ;
- – ;
= – ;
r – ;
w – ;
x – ." \
	--entry-text="")
#    
if [ $? -eq "1" ];then
	exit
fi
#             
if  [[ $mod == *[ugoa]*+*[rwx]* || $mod == *[ugoa]*-*[rwx]* || $mod == *[ugoa]*"="*[rwx]* ]]; then
	chmod $mod "$name"
	zenity --info --title="" --text="'$name'    '$mod'."
elif [[ $mod == +*[rwx]* ]] || [[ $mod == -*[rwx]* ]] || [[ $mod == =*[rwx]* ]]; then
	pre_mod="${mod%[-+=]*}"
	pre_mod+=" "
	if [[ $pre_mod == " " ]]; then
		pre_mod="${pre_mod/ /a}"
		echo "$pre_mod"
		pre_mod+="$mod"
		mod="$pre_mod"
		echo "$mod"
	fi
	chmod $mod "$name"
	zenity --info --title="" --text="'$name'    '$mod'."
else
	zenity --error --title="!" --text="    . ,         ."
fi
;;
esac
cd
cd ""$pr_way"/selection"
#         
next=$(zenity \
	--list --width=400 --height=200 \
	--title="  " \
	--text=", ,         ,
  , ,    ." \
	--column="" \
	" " \
	"   " )
case $next in
	" ") 
start_scr="0"
./key-ru.sh "$pr_way" "$pr_name" "$start_scr"
;;
	"   ") 
start_scr="C"
./key-ru.sh "$pr_way" "$pr_name" "$start_scr"
;;
esac


A link to all our work , for a detailed acquaintance with it, is attached.

All Articles