Linux - Bash Scripts Flashcards
- Write a shell script that renames all files in the current directory to begin with today’s date, in the following format: YYYY-MM-DD. For example, ‘infinity.txt’ to ‘2020-01-01-infinity.txt’
!/bin/bash
d=date +%Y-%m-%d
renameFiles(){
for f in * do echo "$d-$f" mv $f "$d-$f" done } renameFiles
- Write a shell script that asks the user to enter a directory path, and prints the number of files in that directory.
!/bin/bash
dP=/
takeInput () {
echo “Please enter a directory path”;
read dP;
}
takeInput
ls $dP | wc -l
Same as exercise 2, but it recieves the directory name as a parameter to the script
!/bin/bash
dP=/
takeInput () {
echo “Please enter a directory name”;
read dP;
}
takeInput
path=$(find ~ -name “$dP”)
ls $path | wc -l
- Write a shell script that removes all the files under the DIR directory which are larger than X bytes. (X and DIR are parameters)
!/bin/bash
DIR=/
X=99999
curFileSize=1000
inputDIR () {
echo “Please enter a directory name”;
read DIR; #input directory name
DIR=$(find ~ -name “$DIR”); #retrieve path to directory
#DIR=$(ls /home) #become array of files in directory
}
inputX () {
echo “Please enter number of bytes”;
read X;
}
removeFiles(){
pushd $DIR;
for file in * do curFileSize=$(stat -c "%s" $file); if [ $curFileSize -gt $X ]; then echo deleting $file at size $curFileSize; rm $file; fi done popd; }
inputDIR
inputX
removeFiles
- Write a shell script that asks the users for the name of a directory, and prints the name and specific type (whether a regular file, directory or other) for each file in that directory.
!/bin/bash
DIR=/
curFileSize=1000
inputDIR () {
echo “Please enter a directory name”;
read DIR; #input directory name
DIR=$(find ~ -name “$DIR”); #retrieve path to directory
#DIR=$(ls /home) #become array of files in directory
}
printTypes(){
pushd $DIR;
file * popd; }
inputDIR
printTypes
- Write a shell script that converts a string (inserted via a keyboard with an English layout) to one with a hebrew layout, and vice versea
!/bin/bash
word=”template”
newWord=””
declare -A langArr=( [q]=”/” [w]=”’” [e]=ק [r]=ר [t]=א [y]=ט [u]=ו [i]=ן [o]=ם [p]=פ [a]=ש [s]=ד [d]=ג [f]=כ [g]=ע [h]=י [j]=ח [k]=ל [l]=ך [”;”]=x [”’”]=, [z]=ז [x]=ס [c]=ב [v]=ה [b]=נ [n]=מ [m]=צ [,]=ת [.]=ץ [”/”]=. [”/”]=q [”’”]=w [“ק”]=e [“ר”]=r [“א”]=t [“ט”]=y [“ו”]=u [“ן”]=i [“ם”]=o [“פ”]=p [“ש”]=a [“ד”]=s [“ג”]=d [“כ”]=f [“ע”]=g [“י”]=h [“ח”]=j [“ל”]=k [“ך”]=l [“ף”]=”;” [”,”]=”’” [“ז”]=z [“ס”]=x [“ב”]=c [“ה”]=v [“נ”]=b [“מ”]=n [“צ”]=m [“ת”]=”,” [“ץ”]=”.” [”.”]=”/” )
promptWord () {
echo “Please enter a word “;
read word; #input word
}
translateWord () {
for (( i=0; i<${#word}; i++ )); do
#echo “${word:$i:1}”
letter=”${word:$i:1}”
newWord+=${langArr[${letter}]}
done
}
promptWord
translateWord
echo ${newWord}
- Same as exercise 6, but it gets the string from the clipboard שלום
!/bin/bash
word=xclip -o -selection clipboard
newWord=””
declare -A langArr=( [q]=”/” [w]=”’” [e]=ק [r]=ר [t]=א [y]=ט [u]=ו [i]=ן [o]=ם [p]=פ [a]=ש [s]=ד [d]=ג [f]=כ [g]=ע [h]=י [j]=ח [k]=ל [l]=ך [”;”]=x [”’”]=, [z]=ז [x]=ס [c]=ב [v]=ה [b]=נ [n]=מ [m]=צ [,]=ת [.]=ץ [”/”]=. [”/”]=q [”’”]=w [“ק”]=e [“ר”]=r [“א”]=t [“ט”]=y [“ו”]=u [“ן”]=i [“ם”]=o [“פ”]=p [“ש”]=a [“ד”]=s [“ג”]=d [“כ”]=f [“ע”]=g [“י”]=h [“ח”]=j [“ל”]=k [“ך”]=l [“ף”]=”;” [”,”]=”’” [“ז”]=z [“ס”]=x [“ב”]=c [“ה”]=v [“נ”]=b [“מ”]=n [“צ”]=m [“ת”]=”,” [“ץ”]=”.” [”.”]=”/” )
translateWord () {
for (( i=0; i<${#word}; i++ )); do
#echo “${word:$i:1}”
letter=”${word:$i:1}”
newWord+=${langArr[${letter}]}
done
}
translateWord
echo ${newWord}
- Write a shell script that converts a mistakenly inserted string (with the wrong language layout) to the correct form, and changes the keyboard layout to that of the other language.
!/bin/bash
word=”template”
newWord=””
langLayout=””
wordLangLayout=””
letter=”${word:$i:1}”
declare -A langArr=( [q]=”/” [w]=”’” [e]=ק [r]=ר [t]=א [y]=ט [u]=ו [i]=ן [o]=ם [p]=פ [a]=ש [s]=ד [d]=ג [f]=כ [g]=ע [h]=י [j]=ח [k]=ל [l]=ך [”;”]=x [”’”]=, [z]=ז [x]=ס [c]=ב [v]=ה [b]=נ [n]=מ [m]=צ [,]=ת [.]=ץ [”/”]=. [”/”]=q [”’”]=w [“ק”]=e [“ר”]=r [“א”]=t [“ט”]=y [“ו”]=u [“ן”]=i [“ם”]=o [“פ”]=p [“ש”]=a [“ד”]=s [“ג”]=d [“כ”]=f [“ע”]=g [“י”]=h [“ח”]=j [“ל”]=k [“ך”]=l [“ף”]=”;” [”,”]=”’” [“ז”]=z [“ס”]=x [“ב”]=c [“ה”]=v [“נ”]=b [“מ”]=n [“צ”]=m [“ת”]=”,” [“ץ”]=”.” [”.”]=”/” )
promptWord () {
echo “Please enter a word “;
read word; #input word
}
translateWord () {
for (( i=0; i<${#word}; i++ )); do
letter=”${word:$i:1}”
newWord+=${langArr[${letter}]}
done
}
changeToHebrew() {
gsettings set org.gnome.desktop.input-sources sources “[(‘xkb’, ‘il’)]”;
gsettings set org.gnome.desktop.input-sources sources “[(‘xkb’, ‘il’), (‘xkb’, ‘us’)]”;
} changeToEnglish() { gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'us')]"; gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'il'), ('xkb', 'us')]"; }
isEnglish () {
if grep -Fq $word Dictionary.txt
then
#echo changing to hebrew
changeToHebrew
else
#echo changing to english
changeToEnglish
fi
}
promptWord
isEnglish
translateWord
echo ${newWord}
- Write a shell script that binds the last script to the F10 function key.
bind -x ‘ “\e[21”:”bash ./Exc8.sh” ‘
Advanced 1. Write a shell script that searches Google for the string in the clipboard
Advanced 2. Write a shell script that gets a translation for the word in the clipboard from English-to-Hebrew, or vice versa.
Advanced 3. Write a shell script that binds the last 2 scripts to the keys F11 and F12, correspondingly.