mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
384 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 realizes that it needs to run the echo program; it will then search for that program in the directories separated by : in the $PATH, performing a name-based search for the program. When the program is found, it is executed. To determine exactly which program a given program name refers to, you can use the which command. We can also bypass the $PATH by directly specifying the path to the program we want to run.

The paths in the shell are a set 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 speaking, when we run a program, if we do not specify a path, the program will be executed from the current directory.

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

First, the line’s first character d indicates that data is a directory. Then the next nine characters, three characters at a time, form a group. (rwx). They represent the permissions that the file owner (root), user group (root), and everyone else have. A - indicates that the corresponding permission is not granted.

From the above information, only the file owner can modify (w), the data folder (for example, adding or removing files within the folder).

To enter a folder, a user needs the folder and its parent folders’ “execute” permission (represented by x). To list its contents, the user must have read permission (r) on that folder. For files, the meaning of the permissions is similar. Note that the programs in the last group of the /bin directory have x permission for everyone, meaning anyone can execute these programs.

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

Redirection of input/output in Programs#

In the shell, a program has two main “streams”: its input stream and its output stream. When a program tries to read information, it reads from the input stream; when a program prints information, it outputs to the output stream. Typically, a program’s input/output streams are your terminal. That is, your keyboard as input and the display as output. However, we can also redirect these streams!

The simplest redirection is < file and > file. These two forms can redirect the program’s input and output streams respectively to a file:

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

Root user#

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
Install oh-my-zsh and Its Components on Ubuntu
prog-side Installing oh-my-zsh and its components on Ubuntu includes first installing zsh and git, then installing oh-my-zsh via wget. Next, clone the powerlevel10k theme and required plugins, and update the .zshrc file to apply the theme and plugins. Finally, install additional plugins such as zsh-bat and you-should-use, and update the system to ensure everything works properly.
2
Use NAT on Ubuntu to Forward Network Access to a Router
prog-side By modifying Netplan configuration, enabling IP forwarding, and configuring NAT forwarding, Ubuntu can be used to forward network access to a router. The steps include clearing existing configuration, setting a static IP, installing and configuring DHCP services, and verifying that network sharing works correctly.
3
Getting Started with Regular Expressions
prog-side Regular expressions are powerful text pattern-matching tools used to describe and match specific string patterns. They include literal characters, special characters, character classes, and metacharacters, and are widely used in many programming languages and text processing tools. Regular expressions can be used for data validation, text replacement, and substring extraction, offering strong flexibility and functionality. Common metacharacters and features include character matching, quantifiers, boundary matching, and grouping, which help users process text efficiently.
4
The First Round of Selection in the New Era
life With the development of AI technology, the cost of using advanced models may lead to social stratification, where only those with strong financial means can use these models. Although current prices are still acceptable, future price increases may make them unaffordable for most people, thus forming the first round of selection. The author feels anxious about this phenomenon, while also realizing that AI applications have moved beyond programming and into broader industries. Facing the challenges and opportunities of a new world, individuals continue to explore under the momentum of the times.
5
Psychological Record 1
psycho Realizing I’ve been suppressing my emotions, reflecting on how past living habits have led to fear of the outside world and inner stillness. Hoping to find my true self through interacting with the world, while facing the sense of responsibility in choosing a direction and the fear of losing other possibilities. The true self needs to be shaped through exploration, and choosing a future direction means giving up other possibilities.

Table of Contents