Bash Basics Archive

date
Sep 21, 2021
slug
bash-basics-archive
status
Published
summary
Useful & Useless Stuff. manuelish.
type
Post
tags
Linux
Notes
notion image
VE482: Intro to OS, Lab 1 的一部分。
要我来说的话,这个 Lab 里大部分内容,炼过丹的大四学生都应该会。当然,都背出来是不可能的,所以整理下来存个档也很有必要。
就在我看看差不多准备存档的时候,惊闻 GDP 学生不让选 JI 的课了,于是我的 482 也没了。彼时我刚修完自己制造出的一大堆内存泄漏,看着动工了一半的 mumsh 感到有些无语。等回过神来,还得赶紧上 UM 的选课网,把为了上 482 退掉的课加回来——还有五十分钟这学期的选课就结束了。后来和这些 Bash Basics 一起存档的,因此多了几条笑话。作者是谁我也不知道,仅供娱乐。

Useful Stuff: Bash Basics

  • Use the mkdir, touch, mv, cp, and ls commands to:
    • Create a file named test.
    • Move test to dir/test.txt, where dir is a new directory.
    • Copy dir/test.txt to dir/test_copy.txt.
    • List all the files contained in dir.
    •  mkdir lab1-0  mkdir lab1-1  ls  >> lab1-0 lab1-1  cd lab1-0  touch test  ls  >> test  mv ./test ../lab1-1/test.txt  ls  >>  cd ../lab1-1; ls  >> test.txt  cp ./test.txt ./test_copy.txt  ls  >> test.txt test_copy.txt
  • Use the grep command to:
    • List all the files from /etc containing the pattern 127.0.0.1.
    • Only print the lines containing your username and root in the file /etc/passwd (only one grep
    • should be used)
       sudo grep -iR "127.0.0.1" /etc  >>   /etc/hosts:127.0.0.1   localhost   /etc/default/docker:#export http_proxy="http://127.0.0.1:3128/"   /etc/dhcp/dhclient.conf:#prepend domain-name-servers 127.0.0.1;   /etc/dhcp/dhclient.conf:# option domain-name-servers 127.0.0.1;   /etc/security/access.conf:#+:root:127.0.0.1   /etc/speech-dispatcher/modules/ivona.conf:#IvonaServerHost "127.0.0.1"    sudo grep "parallels\|root" /etc/passwd  >>   root:x:0:0:root:/root:/bin/zsh   parallels:x:1000:1000:parallels,,,:/home/parallels:/usr/bin/zsh
  • Use the find command to:
    • List all the files from /etc that have been accessed less than 24 hours ago.
    • List all the files from /etc whose name contains the pattern “netw”.
    • List some of the useless information.
    •  sudo find /etc -atime -1  >>   /etc   /etc/jupyter   ... (there are too much, so omitted here)  sudo find /etc -name "*netw*"  >>   /etc/modprobe.d/blacklist-rare-network.conf   /etc/bluetooth/network.conf   /etc/apparmor/init/network-interface-security   /etc/default/networkd-dispatcher   /etc/init.d/network-manager   /etc/networkd-dispatcher   /etc/systemd/networkd.conf   /etc/systemd/network   /etc/systemd/system/network-online.target.wants   /etc/networks   /etc/netplan/01-network-manager-all.yaml   /etc/sysctl.d/10-network-security.conf   /etc/network sudo find ./ -name enc | cat -head -256 >> "ezcpb"
  • In the bash man-page read the part related to redirections. Explain the following operators >, >>, <<<, >&1, and 2>&1 >. What is the use of the tee command.
    • >: Redirect output to specified file, rewrite this file.
    • >>: Redirect output to specified file, append to the end of it.
    • <<<: Redirect input using specified file as source, stop when a line containing a specified word is seen. The specification will be expanded using ~ expansion, parameter expansion and variable expansion.
    • >&1: Redirect the output to stdout.
    • 2>&1 >: Redirect the content of stderr to stdout.
    • tee: Reads from stdin and write to stdout and files. This is often used within a pipe, writing results to files.
  • Explain the behaviour of the xargs command and of the | operator.
    • xargs: Constructs commands and execute. Used to execute commands (especially with those only taking input from stdin) on a list of arguments.
    • |: The pipe. Redirects the output of the command before it to the input of the command behind it.
  • What are the head and tail commands? How to “live display” a file as new lines are appended?
    • head: Write the first 10 lines of the input file to stdout.
    • tail: Write the last 10 lines of the input file to stdout.
    • "Live Display": Use tail -f.
  • How to monitor the system using ps, top, free, vmstat?
    • ps: Report a snapshot of processes running in system.
    • top: Display real-time view of processes and their information. (htop does this with better visualization)
    • free: Report a snapshot of memory/swap status of the system.
    • vmstat: Report virtual memory statistics such as processes, memory status, etc.
  • What are the main differences between sh, bash, csh, and zsh?
    • sh: The original default shell in Unix systems.
    • csh: The C shell, with features such as aliases, command histor and so.
    • bash: Created by the GNU Project, the default shell in most Linux Distributions today.
    • zsh: The Z shell, contains features of bash, but also has additional features such as spell checking.
  • What is the meaning of $0, $1,..., $?, $!?
    • They are positional arguments for shell scripting. ${N} stands for the N-th argument for a command, while ! in bash is expanded as the process ID of the most recently executed command and ? is executed as the exit status of the last executed command.
  • What is the use of the PS3 variable? Provide a short code example.
    • The PS3 variable is used to select inside a script. Without using this variable the default #? fpr select command will be displayed.
    • Example:
      •  #!/bin/bash  PS3="Select a Manuel's Course (1-4): "  select i in ve472 ve475 ve477 ve482 exit  do   case $i in     ve472) echo "VE472: Methods and Tools for Big Data";;     ve475) echo "VE475: Introduction to Cryptography";;     ve477) echo "VE477: Introduction to Algorithms";;     ve482) echo "VE482: Introduction to Operating Systems"      exit) exit;;    esac  done
  • What is the purpose of the iconv command, and why is it useful?
    • iconv is used to convert some text from one encoding to another. It is useful when handling files with different encoding due to different localization.
  • Given a variable $temp, what is the effect of ${#temp}, ${temp%%word}, ${temp/pattern/string}.
    • ${#temp}: Length of this variable.
    • ${temp%%word}: Content of $temp excluding the longest pattern matching word.
    • ${temp/pattern/string}: Content of $temp, replacing matches of pattern by string.
  • Search online (not on the man pages), how files are organized on a Unix like system. In particular explain what are the following directories used for:
    • /: The root directory.
    • /lib: Libraries as dependency for the binary files in /bin.
    • /usr/lib: Libraries as dependency for the binary files in /usr/bin.
    • /srv: Data for services provided by the system.
    • /sbin: Executables for administrative purposes.
    • /bin: Binary executables, including essential tools such as ls, cp, ...
    • /mnt: Filesystem mount points for physical storages, remote/virtual filesystems, etc.
    • /usr/src: Source files that are system related.
    • /media: Default mount point for removable devices such as USB storage.
    • /dev: Mount points for peripheral devices as a file representation.
    • /boot: Files required for system booting process.
    • /usr/bin: Binary executables distributed with the system but not residing in /bin.
    • /proc: Virtual filesystem with runtime system information.
    • /opt: Reserved directory for softwares and other packages that are not of the default installation of the system distribution.
    • /vmlinuz: The compressed Linux kernel executable that is bootable.
    • /etc: Configuration files for the system.
    • /usr/share: Architecture independent data files of a given OS.
    • /sys: Interface to the kernel providing filesystem-like view of kernel information/configuration.
    • /var: A place for variable files (usually ) such as system logs.
    • /initrd.img: Link to the latest installed Linux initial RAM disk.
    • zp: Zero-padding, you know.
  • Write a game where the computer selects a random number, prompts the user for a number, compares it to its number and displays “Larger” or “Smaller” to the user, until the player discovers the random number initially chosen by the computer.
    •  #!/bin/bash  computer_number=$((($RANDOM)%100))  user_number=101  while [ $computer_number -ne $user_number ] ; do   read -p "Guess a number: " user_number   if [ $computer_number -eq $user_number ] ; then   echo "Correct"   break   elif [ $computer_number -lt $user_number ] ; then   echo "Smaller"   else   echo "Larger"   fi  done  echo "The number is $computer_number"

      Useless Stuff: 482 Jokes

      e087b87cb7923a33f262f29f75e431e93b6f350701d07efd9b6250a01a00c2fd1b5ef67e9a74bd3733876c423b7cd5bc4dab185247d9818274b4982707b9d1b6bd3b2df9c12ee7eb632dbeceec5b18244bdfe1629230df9d4900871f2c598347d94c038b42495f70addede7a2557447f67bbf4e36ed9c6e3f40a1de7485727ea9e2ceb1a8755305e376569b3dfb7412be19c85e4485312b103b7b84a9993b4310738d35eed3f2eb6a9d1fa4a0de39f3dc91195799a0cbbd88b265bde001fdffde65db0e079543f3651adb9df46a18b3e52a2e82b98b9051c25f995c0a443ff334e62758f12525b0e24e425f8c6eef076484760b4dea51c73a9f0e90e75048bd264a0ea0948048bc873b5dd9c7fa653355baae4de01f6900596ead0dccd5fdc1b6c9190978d7555048639cfb56f78124a67ac7119320581e51470ed8113e267545bb0850a093a8613c0db675e3c991e21ecb2121a9c2609538ef369c2907560f081eb3eda0c7c21f4d7aadc80bfb35bb68b5f407f048aad635d509743167d627ea4278c7a3e77ff62dfb973968d70401207384985498f42c7fcc61b4fce24b0a176221da91df300a6c31d716f0b981481782da5d7d7665f6dee6d0685a4c43dac4063d051b487e7656f866c4136ecc5bc312618b46071548a09f03665773f81ace4c978184d659d9138297a20a485abcdbd80bf709f5c68c58b2f58bad5ca094b9d969fb4db2626ccc1c753dac3a57a73c65a91604f53b3da47e0c25337a674fff0ac9d1f545de1d4988167f9ccda1604f8a6a662dc0aec13278ed87d76621522d63928f07cf96afdb4f2fa121c32e9862417e6d699dbcecc0092054cd2ef4bb7f65cb77650df9ef9b894a15c6ac6f4a29271ac6c6da00c8531238d638594bc52620c69772e5a56b938b14a0bdcfafc9ed6cb4476158106dc47c7efd3340529376c86de37c22d3ba90f28fbeee03052b6ebb6e06652c89cbc5f784adbc4b716faaes
对于本文内容有任何疑问, 可与我联系.