This will help myself with the final examnation.

by QiaoGuangtong

Fundamental About Linux

  1. Partition.
    Generally, the partitions in linux, take the ubuntu for instance, four partitions are general, which including “/”, “/boot”, “/swap”, and “/home”, in which home is your users’ directory.

File Command

In linux, the file processing commands are pretty important because all your actions are about file processing. So next term, let us to learn about the commands for processing the files.
At first, you should know that the command is case sensitive, for which you should know which are upper case, and which are lowercase.

  1. ls -list directory contents
    List the files and directory from current directory order by the first case default.
Options Long Option Describe
-a -all List all files
-l -long Display results in long format
-t -time Display result order by time
  1. cp -copy
    This command can copy files or directories. It can be used two different ways.
    cp item1 item2
    to copy the file or directory from item1 to item2, and
    cp item… directory
    to copy multiple items (files or directories) into a directory.
    Example:
    cp -r item1 item2
    Recursively copy directories and their contents. This option is required when item1 is a directory.

  2. mv -move
    This command can move file or directory to a new directory and rename a file
    Example:
    mv item1… item2
    Like cp, move files or recursively move directories and their contents. item1 and item2 can be directory or file.

  3. rm -remove
    This command can remove the directed file or directory
    Example:
    rm -r item
    The same, recursively remove directories and their contents. This option is required when item is a directory.
    rm -f item
    This command will cancel warning when you remove a file or directory and its contents.
    rm -rf item
    This command combined two above.

  4. tar
    This command can zip or unzip the directed files or directories.
    Example:
    tar -czvf item1 item2….
    -czvf create zip visual file (new file name)
    This command can zip item2, which can be file or directory. item2 can be multiple items.
    tar -xvzf item
    This command can unzip item, a zip file.

  5. chmod -change mode
    This command can change permissions of a file or directory.
    Example:
    chmod u+x item add execute on item for current user
    chmod u+r item add read on item for current user
    chmod u+x item add write on item for current user
    chmod g+x item g is group add execute for all users in the same group with current user.
    chmod a+x item a is all add execute for all users

  6. touch
    This command can change the time for visiting and changing. If the file does not exists, create a new file.
    option:
    touch [filename]
    This command can create a new file filename.

  7. find
    This command is the most complex in my class that cannot remind me all the time.
    find ./ -size 5 -name *passwd* -ok cp {} /home ;

Directory Command

  1. mkdir
    This command can create a new directory
    Example:
    mkdir item
    This command creates a directory item in current directory.
  2. cd
    This command can change directory
    Example:
    cd item
    This command changes into item, which is a directory.

User Command

  1. su
    This command can change current user to another.
    Example:
    su sam
    This command change user to sam.
  2. passwd
    This command can update password for a user.
    Example:
    passwd sam
    This command can update password for sam.

Other Command

  1. cat
    This command can display the content of a file. E.g. cat a.txt
  2. echo
    This command can display the content of a string or environment variables and so on. E.g. echo java
  3. ps -ef
    This command can display the processes of current system.
  4. kill -9 [pid]
    This command can kill a process which number is pid.

Vi/Vim

vi is a screen-oriented text editor originally created for the Unix operating system.
vi is a modal editor: it operates in either insert mode (where typed text becomes part of the document) or command mode (where keystrokes are interpreted as commands that control the edit session). When you enter “:” , you will enter last-line mode.

change mode:
i from command mode to insert command
Command mode:
dd delete current line
yy copy current line to buffer
nyy copy n lines to buffer

Last-line mode:
:q quit with no action
:wq save write and quit
:q! force to quit
:x save and quit

. Shell Scripting

  1. Tips
    For a new shell scripting, you can make it according the following steps.
  2. Write your shell scripting file using vi/vim or other editors
  3. Change its permissions so that it can be executed
  4. Execute it
  5. Practice

copy files

  1. copy /etc/passwd, /etc/profile, /etc/shadow to current directory.
    cp /etc/passwd ./cp /etc/profile ./cp /etc/shadow ./

Specific size

  1. Decide if a number is greater than 50
    if [ $1 -gt 50 ] ; then echo “$1 > 50”else echo “$1 <= 50”fi

Add 1-100

  1. Display the sum from 1 to 100
i=0
sum=0
while [ $i -lt 10 ] ; do let i+=1let sum+=$i
done
echo “sum = $sum”

Create Files

  1. Please create 100 files f1 to f100
i=0
while [ $i -lt 100 ] ; do let i+=1touch $i
done

Re-Write cp

  1. Re-Write my copy using two arguments, and decide if the first argument is a directory
if [ -f $1 ] ; thencp $1 $2
elsecp -r $1 $2
fi

Ten Score cp

  1. A perfect shell scripting for rewriting cp
if [ $# -gt 1 ] ; thenif [ -f $1 ] ; then cp $1 $2elif [ -d $1 ] ; thencp -r $1 $2else echo “Source file or directory not found”fi
elseecho “At least two arguments”echo “For example: ./mycp /etc/profile ./ ”
fi

Analyze String

  1. Print every part of the specified symbol split string
t=$IFS
IFS=”:”
data=”root:x:0:0:root:/root:/bin/bash”
for i in $data ; doecho $i
done
IFS=$t

Analyze String 2

  1. Print specific part using number of the specified symbol split string
t=$IFS
IFS=”:”
data=”root:x:0:0:root:/root:/bin/bash”
count=0
for i in $data ; dolet count++[ $count -eq $1 ] && x=$i
done
echo $x
IFS=$t

Analyze String 3

  1. Find the number of specified string in the specified symbol string
t=$IFS
IFS=”:”
data=”liming:wangming:daming:liming:lihua:liming”
count=0
for i in $data ; do [ $i = “liming” ] && let count+=1
done
if [ $count -eq 0 ] ; then echo “Not found”
else echo “Count is $count”
fi
IFS=$t

Analyze String 4

  1. Search specific string and display its count, if it does not exist, append it at the end;
t=$IFS
IFS=”:”
data=”daming:xiaoming:xiaohong:dahong:lihua:zhanghua”
count=0
for i in $data ; do[ $i = $1 ] && let count++
done
if [ $count -eq 0 ] ; thenecho “Not found $1”data = $data:$1
elseecho “count is $count”
fi
echo “new data is $data”
IFS=$t

Read

read -s -p “Enter password: “ pass
if test “$pass” == “password”
then echo “”echo “Password successful”
else echo “”echo “Password failed”
fi

shell function

Like others programming language, shell has function, too.

Define:
[ function ] fun_name [()]
{
action;
[return int;]
}
So you can use function funName() {}, function funName{}, or funName(){} to define a function;
This is instance:

function add()
{sum=$(($1+$2))
return $sum
}
read -p "Enter number1: " item1
read -p "Enter number2: " item2
add $item1 $item2
echo "$item1 + $item2 is $?"

C Programming

GCC

Command gcc can do the work that compile, link, and create executed file.
gcc main.c -o main
This command can create a file main. It can be executed.
gcc main.c -c OR gcc -c main.c
These two commands are the same. They can compile C source file.

Makefile & make

If you write many c source files, you can use make to manage your code easily. Make can gain your time because it can only execute the command you have updated. You should write a file called Makefile, and then use the make command to execute it. And then, you get an executable file.
So, you can write a Makefile like the following:

    main: main.o max.o min.ogcc main.o max.o min.o -o mainmain.o: main.cgcc -c main.cmax.o: max.cgcc -c max.cmin.o: min.cgcc -c min.cclear:rm *.o
/* common.h */
int max(int, int);
int min(int, int);/*main.c*/#include <stdio.h>#include “common.h>int main(){int a, b;printf(“Enter two numbers: “);scanf(“%d %d”, &a, &b);printf(“The max is %d, the min is %d”, max(a, b), min(a, b));return 0;}
/* max.c */
int max(int a, int b)
{return a > b ? a : b;
}/* min.c */
int min(int a, int b)
{return a < b ? a : b;
}

Programming Practice

Random

Write a c source file, output a random double-precision number from 0 to 0.999;

#include <stdio.h>
#include <stdlib.h>
#include <time.h>int main()
{srand(time(0));int r = rand()%1000;double r1 = r / 1000.0;printf(“%.3f\n”, r1);return 0;
}

Fork1

Before showing the practice, I should tell you something else. There is a process, which has a parent process, and when its parent process was killed but itself exists, it became an orphan process. (The orphan process will be re-parentage by kthreadd).

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main()
{pid_t pid;pid = fork();if (pid < 0)printf(“ERROR…\n”);if (pid ==0)printf(“I am a child process..\n”);if (pid > 0)printf(“I am a parent process..\n”);return 0;
}

Fork2

Show pids of the parent process and child process

    #include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(){pid_t pid;pid = fork();if(pid < 0)printf("ERROR..\n");if(pid == 0){printf("I am a child proces.., and my pid is %d, ", getpid());printf("and my parent's pid is %d\n", getppid());sleep(4);printf("I am a child proces.., and my pid is %d, ", getpid());printf("and my parent's pid is %d\n", getppid());}if(pid > 0){sleep(3);printf("I am a parent process, and my pid is %d \n", getpid());}return 0;}

Philosopher’s dining problem

    #include <stdio.h>#include <stdlib.h>#include <signal.h>#include <unistd.h>#include "mysemop.h"#define c_num 5#define p_num 5int main(){int room_sem_id;int chopstick_sem_id[c_num];int i;room_sem_id = CreateSem(c_num - 1);for(i = 0; i < c_num; i++)chopstick_sem_id[i] = CreateSem(1);pid_t pid;pid_t pids[p_num];for(i = 0; i < p_num; i++){pid = fork();if(pid < 0)printf("ERROR...\n");if(pid > 0)pids[i] = pid;if(pid == 0){while(1){printf("%d is thinking...\n", i);sleep(2);printf("%d is hungry...,so he enter room\n", i);Psem(room_sem_id);Psem(chopstick_sem_id[i]);Psem(chopstick_sem_id[(i + 1) % c_num]);printf("%d is eating....\n", i);sleep(c_num - i);printf("%d finished...\n", i);Vsem(chopstick_sem_id[(i + 1) % c_num]);Vsem(chopstick_sem_id[i]);Vsem(room_sem_id);}}}char c;do{c = getchar();if(c == 'q'){for(i = 0; i < p_num; i++)kill(pids[i], 9);}} while(c != 'q');return 0;}

Binding signal

    #include <signal.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <unistd.h>pid_t x;void f1(){printf("Child catched..\n");sleep(1);kill(x,14);}void f2(){printf("Parent catched..\n");}int main(){pid_t pid;pid = fork();if(pid < 0)printf("ERROR...\n");if(pid == 0){signal(14, f1);pause();}if(pid > 0){signal(14, f2);sleep(3);kill(pid, 14);pause();}return 0;}

Summary

This file just service for the Linux final examination. More about Linux, please go to the other, and maybe you can get some answers from the following.

A Website:

https://www.w3cschool.cn/linux/

Linux Examination相关推荐

  1. 嵌入式linux和嵌入式android系统有什么区别和联系?

    转自:http://bbs.eeworld.com.cn/thread-430437-1-1.html 这个问题很多人问,尤其是初入嵌入式的菜鸟.其实大家都认为android是java,已经不是lin ...

  2. 【Linux】RHCE -- RHCSA 认证考试 模拟练习题解析

    RHEL6 的RHCE考试分为RHCSA和RHCE两部分 考试时间: RHCSA 2个半小时  总分300分,210分pass RHCE 2个小时        总分300分,210分pass 考试环 ...

  3. 《Linux就该这么学》培训笔记_ch00_认识Linux系统和红帽认证

    <Linux就该这么学>培训笔记_ch00_认识Linux系统和红帽认证 文章最后会post上书本的笔记照片. 文章主要内容: 认识开源 Linux系统的种类及优势特性 认识红帽系统及红帽 ...

  4. 学习第1天:认识Linux系统和红帽认证

    文章主要内容: 认识开源 Linux系统的种类及优势特性 认识红帽系统及红帽阶梯认证 书本笔记 关于开源 开源软件最重要的特性:低风险.高品质.低成本.更透明. GNU GPL(GNU General ...

  5. Linux Driver教程PPT

    http://blog.csdn.net/jgw2008/article/details/52700662 国内外有很多大学的教学PPT都是公开,这一点十分有利于大家学习. 下面的链接,是来自 旧金山 ...

  6. Android kernel和标准Linux Kernel的差异

    在这里http://www.linuxfordevices.com/c/a/Linux-For-Devices-Articles/Porting-Android-to-a-new-device/发现一 ...

  7. Linux shell 脚本编程-实战篇(二)

    继: Linux shell 脚本编程-实战篇(一) 2. 创建与数据库.Web及电子邮件相关的脚本 2.1 MySQL 数据库 2.1.1 MySQL 数据库安装 到 http://repo.mys ...

  8. linux静态插桩工具PEBIL

    文章目录 引言 论文学习 摘要及简介 设计与实现 插桩代码效率 实验结果及其他 具体使用 引言 PEBIL是San Diego Supercomputer Center某实验室研发的工具,用来对ELF ...

  9. linux实验报告山东科技大学,-linux程序设计实验报告

    -linux程序设计实验报告 (31页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 29.9 积分 实 验 报 告课程名称: Linux程序设计 学 院: ...

最新文章

  1. 阿里达摩院正式加入5G巨头仗:瞄准新基建,成立XG实验室,要与华为战一战
  2. 用python画玫瑰花代码-用python画一朵玫瑰给你
  3. php 计算前几天,php计算几分钟前、几小时前、几天前的几个函数、类分享
  4. 数颜色(洛谷-P1903)
  5. 翻转课堂实践:让学生转换角色试一试
  6. UVA 12898 - And Or 与和或 (思路题)
  7. SpringMVC的URL路径映射@RequestMapping
  8. 网易云音乐ios旧版本安装包_网易云音乐产品分析报告
  9. linux下如何部署php,linux如何部署php
  10. 推荐几个好用的插件(Edge)
  11. 实验一计算机基础和网络知识竞赛,第十三届计算机基础知识竞赛题库.doc
  12. 拼多多搜索热度怎么做|重庆乾胤
  13. web留言板整蛊网站愚人节
  14. L13.linux命令每日一练 -- 第二章 文件和目录操作命令 -- lsattr和file命令
  15. 5G NR PWS系统
  16. 如何对电脑屏幕进行监控?
  17. 【数据库的备份与还原】
  18. 安卓进度条自动增加从1到100完整代码
  19. SCT71403Q,LDO参数
  20. 苹果全球开发者大会推出智能音箱网友吐槽Siri“掉链子”

热门文章

  1. SVN:working copy locked解决方法
  2. 10组团队项目-Beta冲刺-1/5
  3. python selenium处理iframe和弹框(一)
  4. 可以计算阶乘次方的大数计算器
  5. Unable to execute SonarQube: Fail to download libraries from server异常解决
  6. 蜘蛛seo超级外链软件
  7. 2019年2月被举报钓鱼网站
  8. 钻石DIAMOND一词来源于古法文DIAMAUND钻石
  9. 南京工业大学python试卷_南京工业大学施工技术期末试卷及答案
  10. 创建Predix UAA(User Account and Authentication)