1. rename 重命名

# 这里使用 cut 命令截取文件名中想要的一段字符,然后再尾部添加 .s 共同构成新的文件名。
for name in *.s
do
    # 取出第 11 到 17 的字符,并和 ".s" 拼接在一起
    newname="$(echo "$name" | cut -c 11-17)".s
    mv "$name"  "$newname"
done

使用指定变量名:

# 从 var 变量名的右边开始匹配,并删除 pattern 匹配的部分
# var: task1.c
# ${var%.c}: 去除 ".c" 后缀
${var%pattern}

2. 批量创建用户

newusers batchuserfile
# batchuserfile
# pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
phc8166:phc8166::::/stu/phc8166:/bin/bash
tvd6298:tvd6298::::/stu/tvd6298:/bin/bash
sxg8183:sxg8183::::/stu/sxg8183:/bin/bash
rxs9961:rxs9961::::/stu/rxs9961:/bin/bash
axs7268:axs7268::::/stu/axs7268:/bin/bash
kxt4593:kxt4593::::/stu/kxt4593:/bin/bash
safal:safal::::/stu/safal:/bin/bash

3. Batch grading

test=("Saving correctly!" "file1.txt file2.txt merged_file.txt" "append.txt 2 line1 line2")
i=0
for src in *.c; do
    printf "************ task $((i + 2)) ************\n"
    gcc -w $src
    ./a.out <<< ${test[$i]}
    i=$((i + 1))
    printf "\n"
done;

4. 批量 grade homework

test=("2 a 100 b 90" "3 1 2 3" "3 4 5 6" "5 1 2 3 4 5" " 4 1 2 3 4 3")
i=0
for src in *.c; do
    printf "************ task $((i + 2)) ************\n"
    gcc -w $src
    ./a.out <<< ${test[$i]}
    i=$((i + 1))
    printf "\n"
done;

5. DD 命令创建U盘启动盘

diskutil list: 输出挂载 U 盘信息 diskutil umountDisk disk4: 卸载U盘

使用dd命令将下载的Ubuntu Linux镜像恢复到U盘上。一定注意不要把磁盘写错了。不然哭死。

sudo dd if=/Users/sky/Downloads/ISO/ubuntu-17.10-desktop-amd64.iso of=/dev/disk4 bs=50m

# mac
brew install coreutils
sudo gdd if=Downloads/archlinux-2018.10.01-x86_64\(1\).iso of=/dev/disk2 status=progress

完整的输出信息:

~ % diskutil umountDisk disk4
Unmount of all volumes on disk4 was successful
~ % sudo dd if=/Users/sky/Downloads/ISO/ubuntu-17.10-desktop-amd64.iso of=/dev/disk4 status="progress"
Password:
1431+1 records in
1431+1 records out
1501102080 bytes transferred in 906.101477 secs (1656660 bytes/sec)

6. Compile kernel

6.1. prepare

sudo apt update
sudo apt install bison flex
sudo apt install build-essential
sudo apt install libncurses5-dev
sudo apt install fakeroot libssl-dev
sudo apt install libelf-dev

# go to linux source folder
# copy config file to current folder
cp /boot/config-$(uname -r) .config   
make menuconfig # 打开可视化菜单
# 选择 load→OK→Save→OK→EXIT→EXIT

6.2. compile

# compile iso
sudo make -j32

# compile modules
make modules -j32

# install modules
make modules_install

# install kernel
make install

6.3. change bootloader

sudo update-grub

7. linux default to text mode

edit /etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT="text"
GRUB_TERMINAL=console

saving those change:

sudo update-grub
sudo systemctl set-default multi-user.target

8. screen 命令

# 创建 screen
screen -S hanson

# 列出所有 screen
screen -ls

# 返回 screen. id 是 ls cmd 显示 出来的对应screen
screen -r id

9. cpufreq-set all

#!/bin/bash
MAX_CPU=$((`nproc --all` - 1))
for i in $(seq 0 $MAX_CPU); do
    echo "Changing CPU " $i " with parameter "$@;
    cpufreq-set -c $i $@ ;
done
cpufreq-set-all -g powersave

10. Cgroups

Linux Cgroup系列(04):限制cgroup的内存使用(subsystem之memory)

11. 查看打开几个文件

lsof -a  -p 763[pid]

12. replace swap file

# Adding a swap file
sudo dd if=/dev/zero of=/mnt/ssd/swapfile bs=1G count=4
sudo chmod 600 /mnt/ssd/swapfile
sudo mkswap /mnt/ssd/swapfile
sudo swapon /mnt/ssd/swapfile
sudo swapoff -v /swapfile

0 issues reported

results matching ""

    No results matching ""