EXAM - sudo-arshia/tips_and_tricks GitHub Wiki

Q1. Which awk command would calculate the sum of the values in the fifth column of a tab-separated file, and also print the total count of lines where the value in the fifth column is greater than 5?

  • awk 'BEGIN {FS="\t"} {sum+=$5; if($5>5) lineCount++} END {print sum, lineCount}' file.tsv
  • awk '{for(i=1; i<=NF; i++) if($5>5) lineCount++; sum+=$5} END {print sum, lineCount}' file.tsv
  • awk -F"\t" '{if($5>5) lineCount++; sum+=$5} END {print sum, lineCount}' file.tsv
  • awk '{sum+=$5; if($5>5) print lineCount++} END {print sum, lineCount}' file.tsv

Q2. What would the following script's output be if executed with the argument "Hello"?

#!/bin/bash
name=$1
if [ "$name" == "Hello" ](/sudo-arshia/tips_and_tricks/wiki/-"$name"-==-"Hello"-); then
    echo "Greetings, friend!"
elif [ -z "$name" ](/sudo-arshia/tips_and_tricks/wiki/--z-"$name"-); then
    echo "You didn't say anything."
else
    echo "Hello, $name"
fi
  • "Hello, Hello"
  • "Greetings, friend!"
  • "You didn't say anything."
  • The script will fail to execute.

Q3. How would you recursively change the permissions of all directories to 755 and files to 644 in the current directory, except for directories named "confidential" and files named "secret.txt"?

  • chmod -R 755 . && chmod -R 644 . && chmod 700 $(find . -name "confidential") && chmod 600 $(find . -name "secret.txt")
  • find . -type d -not -name "confidential" -exec chmod 755 {} \; && find . -type f -not -name "secret.txt" -exec chmod 644 {} \;
  • chmod 755 $(find . -type d -not -name "confidential") && chmod 644 $(find . -type f -not -name "secret.txt")
  • find . -type d -name "confidential" -exec chmod 755 {} \; && find . -type f -name "secret.txt" -exec chmod 644 {} \;

Q4. How could you correct the error in this script?

#!/bin/bash
file="/path/to/file.txt"
if [ -e $file ]
then
    echo "File exists"
else
    echo "File does not exist"
fi
  • Replace the if [ -e $file ] line with if [ -f $file ]
  • Add the -z flag in the if condition to check for a zero-length string
  • Enclose $file in double quotes in the if condition: if [ -e "$file" ]
  • Replace if [ -e $file ] with if [ $file ](/sudo-arshia/tips_and_tricks/wiki/-$file-)

Q5. Consider the following line of a Bash script:

str="The quick brown fox jumped over the lazy dog"

How would you use Bash string manipulation to extract "brown fox jumped over"?

  • echo "${str:10:21}"
  • echo "${str:10:20}"
  • echo "${str:9:20}"
  • echo "${str:10:19}"

GIT

  1. Consider the following situation: A developer works on a new feature in a branch named 'feature-x'. The developer completes the work, merges the 'feature-x' branch back to the 'master', and then deletes the 'feature-x' branch. Later, the developer discovers a bug that was introduced in 'feature-x'. What command(s) can the developer use to find the exact commit that caused the bug?
  • git checkout feature-x
  • git merge feature-x
  • git bisect start HEAD feature-x
  • None of the above

Explanation: The branch feature-x has been deleted, so it can't be checked out or merged. The git bisect command would also not work since the branch doesn't exist. The developer would have to look through the commit history manually or use the reflog to find the relevant commits.

  1. In a given repository, you want to check out a previous version of your code, but you accidentally delete the commit hash. How can you recover this hash?
  • By using the git reflog command
  • By using the git checkout HEAD command
  • By using the git log --oneline command
  • It's impossible; once a commit hash is deleted, it cannot be recovered

Explanation: The reflog command in Git is used to reference updates (commit hashes) in the Git directory. So, it would be possible to recover the commit hash using git reflog.

  1. You are working on a feature in a branch and made some commits. However, you realize that your feature is flawed and decide to start over, but you want to keep the commit history for future reference. What should you do?
  • git reset --hard HEAD
  • git commit --amend
  • git branch new_branch; git reset --hard HEAD~n
  • git rebase --interactive HEAD~n

Explanation: This question is tricky because it requires the user to understand the need to preserve the commit history while still "starting over" on the current feature. The correct answer creates a new branch with the current commit history and then resets the HEAD of the feature branch to a previous state (n commits back), effectively starting over on the feature branch while preserving the commit history in a new branch.

  1. You want to merge changes from a branch 'feature-xyz' into 'master', but without creating a merge commit. How would you do this?
  • git merge --no-commit feature-xyz
  • git rebase feature-xyz; git checkout master; git merge feature-xyz
  • git checkout feature-xyz; git merge master
  • git checkout master; git merge --squash feature-xyz

Explanation: Rebasing 'feature-xyz' on top of 'master' aligns the feature branch to the tip of the master branch. Then switching to the master branch and merging 'feature-xyz' will just fast-forward the master branch without creating a merge commit.

  1. You have multiple branches in your repository. You want to find out the branches that have commits not merged into the master branch. What command can you use?
  • git branch --merged
  • git branch --contains master
  • git log --branches --not --remotes
  • git branch --no-merged

Explanation: The command git branch --no-merged lists the branches that have commits not merged into the current branch.