一、 常用的命令:
1. 文件
(1) 通过颜色认识文件类型:
颜色 | 灰黑色 | 蓝色 | 绿色 | 红色 | 浅蓝色 |
类型 | 普通文件 | 目录 | 可执行 | 压缩文件 | 链接文件 |
(2) -rw-r--r-- 1 ltrbless ltrbless 96 Jun 26 20:41 1.txt
-rw-r--r-- 1 ltrbless ltrbless 0 Mar 22 02:15 2.txt
drwxr-xr-x 2 ltrbless ltrbless 4096 Mar 1 03:01 Desktop
- :普通文件 d :目录文件(dir) c : 字符设备(char) b :块设备(block) l :链接文件(link)
d rwx r-x r-x
文件类型 所有者权限 同组者权限 其他人权限
(3)文件常用命令 // -i 加上好像都是可以提示用户的
ls 1. ls | ls -l | ls -R | ls -t(按时间排序) | ls -S (按文件的大小排序)
cp 2. cp 文件 目标地方 | cp ./etc ../ 把当前目录下的etc拷贝到上层目录
如果复制文件夹 需要加-r 把其子目录下所有的都复制过去 : cp -r ./etc ./dir1
mv 3. mv 文件 目标地方 | mv -I ./dir1/* ./dir1/test 加-i 提示用户是否覆盖
root@ubuntu:/# mv -i ./dir1/test1/* ./dir1/test2
mv: overwrite './dir1/test2/111.txt'?
ln 4. 一般加 -s(建立符号链接,不是硬链接,符号链接具有更大的灵活度)
ln -s ./dir1 ./dir1/test1/dir1_link //lrwxrwxrwx 1 root root 6 Jul 3 02:52 dir1_link -> ./dir1
/******************* 执行结果 ***********************/
root@ubuntu:/dir1/test1# ls
dir1_link
root@ubuntu:/dir1/test1# ls -l
total 0
lrwxrwxrwx 1 root root 6 Jul 3 02:54 dir1_link -> ./dir1
/********************************************************/
rm 5. rm [参数] 目标 | rm -r(递归删除所有的子目录) | rm -f(忽略不存在的文件,不给出提示) | rm -i(交互方式)
rm -rf ./dir1
tar 6. root@ubuntu:/dir1# tar -czvf yasuo.tar ./*.txt
''' -c(创建新的tar文件) -z(使用gzip过滤归档或者用ungzip解压) -v(列出处理过程中的详细信息) -f(指定新文件名)'''
tar -xzvf yasuo.tar 解压
chmod 7. chmod 776 ./1.txt ( - rwx(7) rwx(7) rw-(6) 1 root root 0 Jun 30 06:14 1.txt)
chmod u+x ./shell_
u : root g : group o : other
touch 8. touch 用来修改文件的访问和访问时间,如果文件不存在则默认创建空文件。
grep 9. grep[参数] 收缩的字符串 文件
***注*** : grep 是搜索的文件里面的东西,如果用管道也可以搜索文件名字(因为进入管道之后就相当于文本了)
example: grep am greptest(在文件greptest里面搜索含有am字符串的行,打印出来)
搜索文件: ls | grep txt (输出包含txt的文件名称)
-n 输出加上行数
find 10. find ./etc -size +100k -name "*.ctb" -ok rm {} \;
//既有名字也有文件大小 -ok代表执行前需要得到用户的确认 和-exec 差不多,多一个用户认可
find ./* -name "999.txt" -exec mv {} ../ \; (找到999.txt移动到上一级目录)
mkdir 11. mkdir -m666 -v dir2 // -v 显示相关信息 , 创建文件夹
pwd 12. 查看当前目录
cat 13. 输出文件内容 cat 文件
echo 14. 输出字符串 echo "String"
tail 15. tail -n 文件 输出该文件的后n行 tail -5 ../../etc/passwd 输出passwd的后5行
head 15. head -n 文件 输出该文件的前n行 head -5 /etc/passwd
wc 16. wc -l 统计行数 wc -c 统计字节个数
>|>> 17. 输出重定向,>文件(没有则创建,会覆盖) >> 文件 (没有则创建,在末尾添加)
ps 18. 查看进程 ps -ef -e : 输出除主进程外的所有进程 -f 以完全格式输出
kill 19. kill(pid, 14) 发送信号量14给进程号为pid的进程
kill -9 进程号 //关闭进程
signal(pid, fun); //把信号量 14 绑定在fun函数上
1. passwd -w 10 // 在距多少天提醒用户修改密码;仅能root权限操作;
2. passwd -l xiaoming //锁定 lock
3. passwd -u xiaoming //解锁 unlock
4. init 6 //重启
5. init 5 //图形界面
6. init 3 //文本界面
7. init 0 //关机
8. man grep //查询grep的用法
7. 变量:
x = 50
echo $x //把x看成变量,输出变量的值
echo x //把x看成字符直接输出
想输出命令的值:
echo $(umask)
/etc/profile //系统配置文件
8. cat /ect/passwd //可以显示文件passwd的内容
9. head -n 5 ect/passwd //显示开头的5行
tail -n 5 ect/passwd //显示后面的5行
10. ls -l | grep ...
11. gnome-terminal // 终端进程
12. ps -ef //查看进程
13.useradd "用户名"
14.su "用户名" // 切换到该用户下
2. vi
:x 保存退出
5dd 删除5行
5yy 复制5行
vim 有三种模式:命令模式、 编辑模式、 底行模式
command mode insert mode last line mode
二、Shell编程:
let命令用于指定算术运算 let i++ *****千万记得加let
/***** 系统变量 *******/
1. $n 第n个参数
2. $# 参数的个数
3. $* 以"($1)($2)..." 形式的所有参数组成的字符串
/****** 条件测试 ********/
1. 文件状态
-f filename # 判断filename是否是文件
-d pathname # 判断pathname是否是目录
2. 逻辑操作
parameter1 -a parameter2 #逻辑与
parameter1 -o parameter2 #逻辑或
!parameter1 #逻辑非
3. 字符串测试
string1 = string2
string1 != string2
string1 < string2
string1 > string2
4. 数值测试
val1 -eq val2 =
val1 -ne val2 !=
val1 -lt val2 <
val1 -le val2 <=
val1 -gt val2 >
val1 -ge val2 >=
/***** if : *******/
if ··· ; then
···
···
elif ··· ; then
···
···
else
···
···
fi
/***** while : ******/
while ··· ; do
···
···
done
/***** for *******/
for (( i=0 ; i<100 ; i++ )) ; do
······
done
------------------------
for i in f1 f2 f3 ; do
echo $i
done
/****** read ***********/
1. read -n 2 x //输入 -n 长度为2 的串
2. read -s x // 隐藏输入的内容
3. read -p "string" //提示用户输入的内容
/*********************************************************************************************************/
文件复制Shell
/*********************************************************************************************************/
Task1:
etc/passwd || profile || shadow(passwd de ying zi)
建立一个脚本文件backup,备份etc/passwd || profile || shadow 备份到 /home 下 (期末考试)
./mycp 源文件 目的地 <==> 实现的功能和cp的功能一样
$1 $2
Step :
(1) vi backup
(2) i (insert)
(3) cp $1 $2
(4) 按Esc
(5) 按 :
(6) x + 回车 (x是保存)
(7) chmod u+x backup
(8) ./mycp 源文件 目的地 (执行backup脚本)
-------------------------------------------------------------------------------------------
Task2:
cp -r /etc /home //拷贝目录 -r recursive(递归) ls -r
cp /etc/passwd /home //拷贝文件
if $1 is file ; then
cp $1 $2
else
cp -r $1 $2
-------------------------------------------------------------------------------------------
Task3:
./mycp X Y // 把X拷贝到Y 不管X是文件还是目录可以直接用 ./mycp 语句
Shell代码:
if [ -f $1 ] ; then //判断 $1 是否是文件还是文件目录
cp $1 $2
else cp -r $2 $3
fi
-------------------------------------------------------------------------------------------
Finally:
if [ $# -gt 1];then # $# 代表输入参数的个数
if [ -f $1 ];then # 判断 $1 是否是文件还是 -r
cp $1 $2
elif [ -d $2 ];then # 判断 $2 是否是目录,因为只有目录才是 -d
cp -r $2 $3
else
echo "source file not found"
fi
else
echo "at least two arguments"
echo "For example: ./mycp /etc/passwd /home"
fi
/******************************************************/
read 读取用户输入内容
/******************************************************/
read -p "输入密码" -s pass #-p 设置用户提示信息,该提示信息将会显示在光标前。 -s 隐藏输入内容
if [ "$pass" = "jerry" ] ; then
echo "密码验证成功"
fi
read -p "输入密码" -sn 4 pass # -s 隐藏输入内容 -n 输入长度为n的字符串
#这里就是当输入的字符串为 4 个时自动结束
if [ "$pass" = "jerry" ] ; then
echo "密码验证成功"
fi
/******************************************************/
求 1 - 100 的和
/******************************************************/
x=0
sum=0
while [ $x -lt 100 ] ; do
let x+=1
let sum+=$x
done
echo "1 - 100 sum is :" $sum
/******************************************************/
创建x个文件 ./create_file x
/******************************************************/
while循环创建文件
i=0
while [ $i -lt $1 ] ; do
let i+=1
touch f$i
done
------------------------------------------------------
for循环创建文件:
for (( i=0 ; i<100 ; i++)) ; do
touch f$i
done
/******************************************************/
/etc/passwd 解析
/******************************************************/
/etc/passwd中一行记录对应着一个用户,每行记录又被冒号(:)分隔为7个字段,
其格式和具体含义如下:
root : x : 0 : 0 : root : /root : /bin/bash
root : x : 0 : 0 : root : /root : /bin/bash
用户名 : 口令 : 用户标识号 : 组标识号 : 注释性描述 : 主目录 : 登录Shell
“用户名”
代表用户账号的字符串。通常长度不超过8个字符,并且由大小写字母和/或数字组成。登录名中不能有冒号(:),因为冒号在这里是分隔符。为了兼容起见,登录名中最好不要包含点字符(.),并且不使用连字符(-)和加号(+)打头。
“口令”
一些系统中,存放着加密后的用户口令字。虽然这个字段存放的只是用户口令的加密串,不是明文,但是由于/etc/passwd文件对所有用户都可读,所以这仍是一个安全隐患。因此,现在许多Linux系统(如SVR4)都使用了shadow技术,把真正的加密后的用户口令字存放到/etc/shadow文件中,而在/etc/passwd文件的口令字段中只存放一个特殊的字符,例如“x”或者“*”。
“用户标识号”用户 ID(UID)
是一个整数,系统内部用它来标识用户。一般情况下它与用户名是一一对应的。如果几个用户名对应的用户标识号是一样的,系统内部将把它们视为同一个用户,但是它们可以有不同的口令、不同的主目录以及不同的登录Shell等。通常用户标识号的取值范围是0~65535。0是超级用户root的标识号,1~99由系统保留,作为管理账号,普通用户的标识号从100开始。在Linux系统中,这个界限是500。
“组标识号”用户的组 ID(GID)
字段记录的是用户所属的用户组。它对应着/etc/group文件中的一条记录。
“注释性描述”
字段记录着用户的一些个人情况,例如用户的真实姓名、电话、地址等,这个字段并没有什么实际的用途。在不同的Linux系统中,这个字段的格式并没有统一。在许多Linux系统中,这个字段存放的是一段任意的注释性描述文字,用做finger命令的输出。
“主目录”
也就是用户的起始工作目录,它是用户在登录到系统之后所处的目录。
/******************************************************/
把/etc/passwd的第一行,每一列单独输出
/******************************************************/
t = $IFS
IFS = ":"
IFS = $t
针对系统变量常常使用的策略,避免修改了系统变量的初始值
-----------------------------------------------
data="root:x:0:0:root:/root:/bin/bash"
t=$IFS
IFS=":"
for i in $data ; do
echo $i
done
IFS=$t
------------------------------------------------
输出指定的列
data="root:x:0:0:root:/root:/bin/bash"
x=0
t=$IFS
IFS=":"
for i in $data ; do
let x+=1
if [ $x -eq $1 ] ; then
echo $i
fi
done
IFS=$t
data="root:x:0:0:root:/root:/bin/bash"
count=0
t=$IFS
IFS=":"
for i in $data ; do
let count+=1
[ $count -eq $1 ] && x=$i //换了一种方式
done
echo $x
IFS=$t
-------------------------------------------------------
f1(){
data="root:x:0:0:root:/root:/bin/bash"
count=0
t=$IFS
IFS=":"
for i in $data ; do
let count+=1
[ $count -eq $1 ] && x=$i
done
echo $x
IFS=$t
}
f1 $1
f1 $2
/******************************************************/
查找字符串
/******************************************************/
data="root:x:0:0:root:/root:xiaoming:I:amL:linux"
t=$IFS
IFS=":"
count=0
for i in $data ; do
[ $i = $1 ] && let count++
done
if [ $count -ne 0 ] ; then
echo "find"
else echo "not find"
fi
IFS=$t
------------------------------------------------------
找一个字符串中有没有某个子串,没有的话就把子串加入进来
/******************************************************/
判断分数的等级
/******************************************************/
read -p "please input score : " score
if [ $score -gt 100 -o $score -lt 0 ] ; then
echo "input error !"
elif [ $score -ge 90 ] ; then
echo "A"
elif [ $score -ge 80 ] ; then
echo "B"
elif [ $score -ge 70 ] ; then
echo "C"
elif [ $score -ge 60 ] ; then
echo "D"
else echo "E"
fi
---------------------------------------------------------
quit=0
while [ $quit -eq 0 ] ; do
echo "Please input the score : "
read score
case $score in
100|9[0-9]) echo "A";;
8[0-9]) echo "B";;
7[0-9]) echo "C";;
6[5-9]) echo "Dmax";;
6[0-4]) echo "Dmin";;
[0-5][0-9]) echo "E";;
quit|q|qu|qui)
echo "Quit !"
quit=1;;
*) echo "Wrong Input !";;
esac
done
/******************************************************/
写makefile
/******************************************************/
https://blog.youkuaiyun.com/qq_35451572/article/details/81092902
main.c 分割成3个c文件,1个h文件
-------------------------------
main.c
#include<stdio.h>
#include"common.h"
int main()
{
int a, b, c; scanf("%d %d", &a, &b);
printf("max is %d\n", max(a, b));
printf("min is %d\n", min(a, b));
}
-------------------------------
min.c
int min(int a, int b)
{
return a < b ? a : b;
}
-------------------------------
max.c
int max(int a, int b)
{
return a > b ? a : b;
}
-------------------------------
common.h //头文件放函数和类的声明
int max(int a, int b);
int min(int a, int b);
-------------------------------
gcc main.c min.c max.c -o main
编译:
gcc main.c -c //只编译,生成二进制目标文件——> main.o
gcc max.c -c
gcc min.c
链接:
gcc main.o max.o min.o -o main
/****** Makefile ************/
main:main.o max.o min.o
gcc main.o max.o min.o -o main
main.o:main.c
gcc main.c -c
max.o:max.c
gcc max.c -c
min.o:min.c
gcc min.c -c
clear:
rm -rf *.o
/*****************************************/
umask
/****************************************/
关键:遮挡
目录系统预设的默认权限是 : 777 rwxrwxrwx
文件系统预设的默认权限是 : 666 rwxrwxrwx
root用户的umask为 0022
所以root用户创建的默认的目录权限为:
777 111 111 111
022 000 010 010
final 111 101 101 -> rwx r_x r_x : 755
所以root用户创建的默认的文件权限为:
777 110 110 110
022 000 010 010
final 110 100 100 -> rw_ r__ r__ : 644
普通用户的 umask 为 0002
所以以普通用户创建的 目录权限和文件权限 也就很清楚了
三、c语言编程
/*****************************************/
生成随机数
/****************************************/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(0));
int r = rand();
printf("%d\n", r);
}
-------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(0));
int r = rand() % 1000;
double rd = (double)r / 1000.0;
printf("%.3f\n", rd);
}
/***************************************/
孤儿进程
/***************************************/
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t x;
printf("Now my pid is : %d\n", getpid());
x = fork();
if(x < 0)
{
printf("error\n");
}
else if(x == 0)
{
printf("I am child ... PID = %d\n", getpid());
}
else
{
printf("I am parent ... PID = %d\n", getpid());
}
}
运行结果:
Now my pid is : 5772
I am parent ... PID = 5772
I am child ... PID = 5773
----------------------------------------------------
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int x;
x = fork();
if(x < 0)
{
printf("error !");
}
if(x == 0)
{
sleep(3);
printf("I am orphan ...\n");
}
if(x > 0)
{
printf("I am parent ...\n");
}
}
--------------------------------------------------
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int x;
x = fork();
if(x < 0)
{
printf("error !");
}
if(x == 0)
{
printf("my parent PID is : %d\n", getppid());
sleep(3);
printf("my PID is : %d\n", getpid());
printf("I am orphan ...\n");
printf("my parent PID is : %d\n", getppid());
}
if(x > 0)
{
sleep(1);
printf("I am parent ...\n");
}
}
输出结果:
my parent PID is : 5959
I am parent ...
my PID is : 5960
I am orphan ...
my parent PID is : 1
------------------------------------------------
/********************************************/
信号量
/*********************************************/
子进程绑定14号信号到函数f1,父进程绑定14号到f2,
父进程先向子进程发送14号信号,子进程收到14号信号
后,打印一句话后再向父进程发送14号
#include<stdio.h>
#include<signal.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
pid_t x;
void f1()
{
printf("child catch ... \n");
kill(x,14);
}
void f2()
{
printf("parent catch ...\n");
}
int main()
{
pid_t pid;
pid = fork();
if(pid < 0)
{
printf("error !");
}
if(pid == 0)
{
x = getppid();
signal(14, f1); //把信号量14和f1函数绑定在一起,只要来了信号量14,那么就执行f1
pause();
}
if(pid > 0) //父进程执行的比子进程快,因此要用sleep
{
signal(14, f2);
sleep(3);
kill(pid, 14); //主进程 fork()返回新建的子进程ID, 子进程fork()返回0
pause();
}
}
/***************************************************/
哲学家进餐问题
/***************************************************/
#include "mysemop.h"
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#define c_num 5 //筷子
#define p_num 5 //哲学家
int main(int argc, char argv[])
{
int i = 0;
int root_sem_id = 0;
int chopsticks_sem_id[c_num];
int room_sem_id = CreateSem(c_num - 1);
for (i = 0; i < c_num; i++) {
chopsticks_sem_id[i] = CreateSem(1);
}
pid_t pid;
pid_t chldpid[p_num];
char ch;
for (i = 0; i < p_num; i++) {
pid = fork();
if (pid < 0) {
fprintf(stderr,"When fork philosopher %d Failed!\n", i);
exit(-1);
}
if (pid == 0) { //子进程
while (1) {
printf("%d thinking ...\n", i);
sleep(1);
Psem(room_sem_id);
Psem(chopsticks_sem_id[i]);
Psem(chopsticks_sem_id[(i + 1) % 5]);
printf("%d are eating ...\n", i);
sleep(5 - i);
Vsem(chopsticks_sem_id[(i + 1) % 5]);
Vsem(chopsticks_sem_id[i]);
Vsem(room_sem_id);
sleep(1);
printf("%d left the room ...\n", i);
}
}
if(pid > 0) //记录子进程的进程号
{
chldpid[i] = pid;
}
}
do //父进程在这里呆着,管着子进程,然后还可以把他的子进程都结束
{
ch = getchar();
if (ch == 'q')
{
for (i = 0; i < 5; i++)
kill(chldpid[i], 9);
}
} while (ch != 'q');
}
1753





