Bash Basics Archive
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.
  • Use the grep command to:
    • List all the files from /etc containing the pattern
    • Only print the lines containing your username and root in the file /etc/passwd (only one grep
    • should be used)
  • 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.
  • 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:
    • 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.
      • Useless Stuff: 482 Jokes

    © Enoch2090 2018-2024