Once at my job I had to find cool Linux engineer. So I made several practical tasks for candidate. Who had to solve them during the interview. The engineer has been found and then I can leave it here with examples of my own solution. If you can make it better or you have any suggestions please feel free to inform me via links on the contact page.

1. CLI magic

  • make the folder named ‘files’ in the homedir. and create there files named with all posible combinations of 3 Latin letters (from aaa to zzz). make the executable these files that matches with commands.

[ Caution: spoiler! ]

mkdir files && cd files && touch {a..z}{a..z}{a..z}
echo $PATH | sed 's/:/\n/g' | xargs -I{} find {} -type f -name '???' -printf '%f\n' | xargs -I{} chmod +x {}
  • make the folder named ‘files’ with 10 subfolders with names from 0 to 9, which have 100 subfolders from 00 to 99. each of these subfolders should have 10 files named from 0 to 9 and 10 files named from 100 to 109.

[ Caution: spoiler! ]

mkdir -p files/{0..9}/{00..99}
find files/ -type d -name ?? -exec touch {}/{100..109} \; -exec touch {}/{0..9} \;

2. scripting

  • make a script which checks speed of resolving of domain tcp22.sh of public DNS servers (208.67.222.222, 8.26.56.26, 84.200.69.80). put the fastest in /etc/resolve.conf

[ Caution: spoiler! ]

#!/usr/bin/env bash

dns=(208.67.222.222 8.26.56.26 84.200.69.80)
test_name=helloasterisk.ru

check(){
    time=0
    for i in {0..9} ; do
        time=$(( time + $(dig @$1 $2 A | grep -Po '(?<=Query time: )[0-9]+(?= msec)') ))
    done
    echo $(( time / 10 ))
}

replace(){
    cp /etc/resolv.conf /etc/resolv.conf.bak
    echo "nameserver $1" > /etc/resolv.conf
}

min=0
for srv in ${!dns[*]} ; do
    cur=$(check ${dns[$srv]} $test_name)
    if [[ $cur -lt $min || $min -eq 0 ]]; then
        min=$cur
	res=$srv
    fi
done
replace ${dns[$res]}
  • make a script which outputs path of files in folder ‘files’, in which sum of odd numbers equals sum of even numbers. (for example files/3/57/5 3+7=5+5)

[ Caution: spoiler! ]

#!/usr/bin/env bash

happy(){
    fname=$1
    digits=${fname//[^0-9]/}
    sum1=0
    sum2=0
    i=1
    while [ "$digits" != "" ]; do
        if [[ $((i % 2)) -eq 1 ]]; then
            sum1=$((sum1 + $(echo "$digits" | cut -c1 )))
        else
            sum2=$((sum2 + $(echo "$digits" | cut -c1 )))
        fi
        digits=$( echo "$digits" | cut -c2- )
        i=$((i + 1))
    done
    if [[ $sum1 -eq $sum2 ]]; then
        return 0 
    else
        return 1
    fi
}

scan() {
    for e in "$1"/*; do
        if [ -d "$e" ]; then
            scan "$e"
        else
            if happy "$e"; then
                echo "$e"
            fi
        fi
    done
}

scan ./

3. Linux settings

  • change ssh port from 22 to 80 on clean installed CentOS 7

[ Caution: spoiler! ]

vi /etc/ssh/sshd_config
yum install policycoreutils-python
semanage port -m -t ssh_port_t -p tcp 80
firewall-cmd --permanent --service=ssh --add-port=80/tcp
firewall-cmd --permanent --service=ssh --remove-port=22/tcp
firewall-cmd --reload