mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
378 words
2 minutes
missing-semester-class01
2023-01-11

class01

Shell#

Features#

They allow you to run programs, give them input, and inspect their output in a semi-structured way.

Usage#

date # 显示时间
echo hello # 输入参数‘hello’并输出
echo "hello world"
echo $PATH # 输出环境变量¥PATH
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

When we execute the echo command, the shell recognizes that it needs to run the echo program. It then searches the sequence of directories in $PATH, separated by :, for a program with that name. Once found, the program is executed. To determine which specific program a program name refers to, you can use the which program. We can also bypass $PATH by directly specifying the path to the program we want to execute.

A path in the shell is a sequence of directories separated by / on Linux and macOS, and by \\ on Windows. / is the root directory.

pwd # 获取当前路径
cd /home # 根目录下的home文件夹
cd ./home # 当前目录下的home文件夹
cd .. # 上级目录

Generally, when we run a program without specifying a path, the program executes in the current directory.

ls # 查看目录文件
ls --help
ls -l /home
# drwxr-xr-x 4 root root 4096 11月 30 21:01 data

First, the first character on this line, d, indicates that data is a directory. The next nine characters are divided into groups of three (rwx). They represent the permissions of the file owner (root), the group (root), and everyone else, respectively. A - indicates that the corresponding user does not have that permission. From the information above, only the file owner can modify (w) the data directory—for example, by adding or deleting files within it. To enter a directory, a user must have “search” permission, represented by the “execute” permission (x), for both that directory and its parent directories. To list its contents, the user must have read permission (r) for the directory. Permissions have similar meanings for files. Note that programs in the /bin directory include the x permission in the final group, which represents everyone else, meaning that anyone can execute these programs.

mv test ./data/test.txt
cp test.c test01.c
mkdir test
man ls

Connecting Programs#

In the shell, programs have two primary “streams”: an input stream and an output stream. When a program attempts to read information, it reads from its input stream; when it prints information, it writes to its output stream. Normally, a program’s input and output streams are connected to your terminal—your keyboard provides input, and your display shows output. However, we can also redirect these streams! The simplest forms of redirection are < file and > file. These commands redirect a program’s input and output streams, respectively, to files:

echo hello > hello.txt
cat hello.txt
cat < hello.txt
cat < hello.txt > hello2.txt
cat hello2.txt
ls -l / | tail -n1
curl --head --silent baidu.com | grep --ignore-case content-length | cut --delimiter=' ' -f2

The Root User#

Access denied (permission denied) sudo

Exercises#

# 第二题
cd /tmp
mkdir missing
ls | grep missing
# 第三题
man touch
# 第四题
touch ./missing/semester
# 第五题
echo '#! /bin/sh' > ./missing/semester
echo 'curl --head --silent <https://baidu.com>' | tee -a ./missing/semester
cat ./missing/semester
# 第六题
./missing/semester
ls -l ./missing
# -rw-r--r-- 1 root root 62 1月 11 21:46 semester
# 该文件无x 运行权限
# 第七题
man chmod
# 第八题
chmod +x ./missing/semester
# 第九题
./semester | grep Date > ./last-modified.txt
cat last-modified.txt
Share

If this article helped you, please share it with others!

missing-semester-class01
https://dreaife.tokyo/en/posts/smart-shell-guide/
Author
dreaife
Published at
2023-01-11
License
CC BY-NC-SA 4.0

Some information may be outdated

Related Posts Smart
1
Installing Oh My Zsh and Its Plugins on Ubuntu
cs-base To install Oh My Zsh and its components on Ubuntu, first install Zsh and Git, then install Oh My Zsh using wget. Next, clone the Powerlevel10k theme and required plugins, and update the .zshrc file to enable them. Finally, install additional plugins such as zsh-bat and you-should-use, and update the system to ensure everything works properly.
2
Forwarding Network Traffic to a Router via NAT on Ubuntu
cs-base Configure Netplan, enable IP forwarding, and set up NAT to allow Ubuntu to share its network connection with a router. The process includes clearing existing configurations, assigning a static IP address, installing and configuring a DHCP service, and verifying that network sharing works correctly.
3
CSAPP Chapter 1: A Tour of Computer Systems
cs-base A computer system consists of hardware and system software and runs applications through a program life cycle of creation, execution, output, and termination. Information is made of bits and context, and programs are transformed into executables by the compilation system. The processor reads and executes instructions, using caches to improve performance. The operating system manages hardware and provides abstractions such as processes and virtual memory to support concurrency and parallelism. Abstraction is a core concept in computer science, and virtual machines provide an abstraction of an entire computer.
4
About Errors When Using pandas.to_datetime with Different Time Formats
cs-base When using the pandas to_datetime function, errors may occur if date values use different formats. Setting the format parameter to 'mixed' can solve issues caused by inconsistent formats. Example code shows how to handle invalid date formats and successfully convert values to datetime.
5
Configure Docker + code-server on Alibaba Cloud to Build an Online Compiler
cs-base Set up an online compiler by installing Docker and code-server. The process includes installing Docker, configuring an Alibaba Cloud mirror, running Nginx, installing and configuring code-server, and setting up a C/C++ build environment, then successfully running test code.

Table of Contents