Linux - Bash Scripts Flashcards

1
Q
  1. 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’
A

!/bin/bash

d=date +%Y-%m-%d

renameFiles(){

for f in *
do
	echo "$d-$f"
	mv $f "$d-$f"
done } renameFiles
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Write a shell script that asks the user to enter a directory path, and prints the number of files in that directory.
A

!/bin/bash

dP=/
takeInput () {
echo “Please enter a directory path”;
read dP;
}

takeInput
ls $dP | wc -l

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Same as exercise 2, but it recieves the directory name as a parameter to the script

A

!/bin/bash

dP=/
takeInput () {
echo “Please enter a directory name”;
read dP;
}

takeInput
path=$(find ~ -name “$dP”)
ls $path | wc -l

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Write a shell script that removes all the files under the DIR directory which are larger than X bytes. (X and DIR are parameters)
A

!/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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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.
A

!/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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. 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
A

!/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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Same as exercise 6, but it gets the string from the clipboard שלום
A

!/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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. 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.
A

!/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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Write a shell script that binds the last script to the F10 function key.
A

bind -x ‘ “\e[21”:”bash ./Exc8.sh” ‘

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Advanced 1. Write a shell script that searches Google for the string in the clipboard

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Advanced 2. Write a shell script that gets a translation for the word in the clipboard from English-to-Hebrew, or vice versa.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Advanced 3. Write a shell script that binds the last 2 scripts to the keys F11 and F12, correspondingly.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly