华南农业大学 Linux 实验大全

实验1

要求掌握

touch, echo, tail, find, grep, date, tar, ls, awk, mv,
# 新建一个exam1.txt的文件
touch exam1.txt

# 利⽤重定向把字符串 1234567890 追加到 exam1.txt 的末尾;
echo 1234567890 >>exam1.txt

#把/etc/passwd的最后5⾏追加到 exam1.txt 中;
tail -n 5 /etc/passwd >>exam1.txt

# 搜索 /usr 下所有以 xml 结尾的⽂件(不包含⽬录),并把路径中含有"codes"的⽂件路径追加到exam1.txt 中;
find /usr -name "*.xml" | grep codes >>exam1.txt

# 把当前时间按照 年-⽉-⽇ 时:分:秒 的格式追加到 exam1.txt 中。如:2020-11-23 09:32:43;
date +'%Y-%m-%d %H:%M:%S'>>exam1.txt

# 对⽬录 /var/log 进⾏压缩⽣成名为 log.tar.gz ⽂件;
tar -czvf log.tar.gz /var/log

# 通过 ls 命令以⻓格式的形式查看 log.tar.gz 的信息,并把信息追加到 exam1.txt 中;
ls -l log.tar.gz

# 利⽤ awk 命令提取上⼀步中打印的 log.tar.gz 的 ⽂件类型和权限信息 追加到 exam1.txt
中;
ls -l log.tar.gz | awk '{print $1 $2}' >>exam1.txt

# 把 exam1.txt 重命名为 学号.txt 然后提交
mv exam1.txt 2018238907.txt

实验2

要求掌握:

mkdir, touch, cat, head, useradd, userdel, chown, chmod, chgrp, su, ls[-lRSh], ln, rm,
# 登录 root 账号,查看 /tmp ⽬录下是否存在⼦⽬录 myshare,如果没有则建⽴该⽬录;
mkdir myshare

# 在 myshare ⽬录下创建⼀个名为“学号”的⽂件夹和⼀个名为 exam2.txt 的⽂件;
mkdir 201825666
touch exam2.txt

# 创建⼀个名字为 test 的新⽤户,并指定uid为1024;
useradd -u 1024 test

# 把 /etc/passwd 和 /etc/shadow 含有⽤户 test 信息的⾏追加到 exam2.txt ⽂件中;
cat /etc/passwd | grep test >>exam2.txt
cat /etc/shadow | grep test >>exam2.txt

# 把 /etc/passwd 前13⾏的内容 追加到 myshare ⽬录下名为 tmp.txt的⽂件中;
head -n 13 /etc/passwd >>myshare/tmp.txt

# 把 myshare ⽬录下的所有⽂件和⼦⽬录的内容以⻓格式(含权限信息)的⽅式追加到 exam2.txt 中;
ls -lR myshare >>exam2.txt

# 把 myshare ⽬录及其⽬录下的所有⽂件和⼦⽬录的拥有者设置为⽤户 test ,组改为 mail;
chown -R test myshare
chgrp -R mail myshare

# 利⽤su命令切换到⽤户 test 账号;
su test
	
# 保存 hello.sh 后,给予 hello.sh 拥有者可读、可写和可执⾏的权限,同组可读可执⾏,其他⼈可执⾏权限;
chmod 751 hello.sh

# 以⻓格式的形式查看 hello.sh 的⽂件权限信息,并把输出内容追加到 exam2.txt;
ls -l hello.sh>>exam2.txt

# 输⼊ ./hello.sh 执⾏脚本,查看输出结果。并把输出结果追加 exam2.txt;
./hello.sh >>exam2.txt

# 进⼊⽤户 test 的⽤户主⽬录,在这个⽬录下创建 hello.sh 的软链接,同时拷⻉ hello.sh 到该⽬录下并改名为 hello.sh.bak;
cd /home/test
ln -s /tmp/myshare/hello.sh /home/test/hello.sh
cp /tmp/myshare/hello.sh hello.sh.bak

# 以⻓格式形式查看⽤户 test 主⽬录下的所有⽂件(含隐藏⽂件)并把结果追加到 exam2.txt中;
ls -lRa /home/test >>exam2.txt

# 执⾏⽤户 test 主⽬录下的hello.sh⽂件,查看链接是否正常;
/home/test/hello.sh

# 退出⽤户 test 帐号,回到root帐号
su root

# 以⻓格式形式查看⽤户 test 主⽬录下的所有⽂件(含隐藏⽂件)并把结果追加到 exam2.txt中;
ls -lRa /home/test >>exam2.txt

# 从 /usr 开始查找后缀名为.conf的所有⽂件(不包含⽬录),把输出结果追加到 exam2.txt中;
find /usr -name "*.conf" -type f >>exam2.txt

# 利⽤命令从上⼀步找到的conf⽂件中找出⽂件容量最⼤的⽂件,并把这个⽂件以⻓格式形式追加到exam2.txt 中 ;(倒引号)
ls -lSh `find /usr -name "*.conf" -type f` | head -n 1 >>exam2.txt

# 统计出系统中有多少个⽤户帐号,把数量追加到 exam2.txt 中;
cat /etc/passwd | wc -l >>exam2.txt

# 删除⽤户test 的所有内容(包括主⽬录)
userdel -r test

# 删除/tmp/myshare⽬录
rm -rf /tmp/myshare


实验3

我使用的是 Windows10 下的 WSL-Ubuntu18.04

要求掌握

# 关闭防火墙
systemctl stop firewall

# 安装并启动telnet服务(xinetd模式);
sudo apt install -y xinetd telnetd
service xinetd start
service xinetd stop
service xinetd restart
vim /etc/xinetd.d/telnet

# 测试 telnet
telnet localhost

# 安装并启动ftp服务(stand alone模式);
sudo apt install -y vsftpd
service xinetd restart
service vsftpd start
ps -ef | grep vsftp

# 创建名为 a+学号 的账号,并在sudoers⽂件中添加该⽤户信息,使得该⽤户可以使⽤sudo命令;
useradd a2018988888
passwd a2018988888
chmod u+w /etc/sudoers
# 增加“a2018988888 ALL=(ALL) ALL”一行

# 利⽤chkconfig命令查看 telnet状态,并把结果追加到 root主⽬录下的 exam3.txt ⽂件中;
chkconfig --list | grep telnet >>exam3.txt

# 把vsftpd的进程信息追加到 exam3.txt ⽂件中(注意去掉grep本身那⼀条记录);
ps -ef | grep vsftpd | grep -v "grep">>exam3.txt

# 把telnet的配置⽂件的内容追加到 exam3.txt 中;
cat /etc/xinetd.d/telnet >>exam3.txt

# 在window命令⾏下,使⽤ a+学号 帐号 telnet 登录linux,把root主⽬录下的 exam3.txt ⽂件复制到当前⽤户的主⽬录下 tmp.txt ,并把 tmp.txt 的所属⽤户和所属组改为当前⽤户和当前⽤户所属的组;
telnet 192.168.1.107
sudo cp exam3.txt /home/jt/tmp.txt
sudo chown jt /home/jt/tmp.txt
sudo chgrp jt /home/jt/tmp.txt

# 在上⼀步的telnet登录中,把当前路径和当前时间追加到 tmp.txt 中;
sudo pwd>>tmp.txt
date +"%H:%M:%S">>tmp.txt

# 在上⼀步的telnet登录中,把/etc/passwd⽂件中的第1、3、4字段内容(⽤户名、uid和gid信息)追加到 tmp.txt 中;


# 在新打开的cmd窗⼝中使⽤ftp命令进⾏匿名登录(⽤户名:ftp,密码为:空);
ftp 192.168.1.107

# 退出ftp⽤户登录
ftp>close
ftp>quit

# 登录成功后,切换到/usr⽬录,并查看⽬录内容。发现⽤户可以访问其他⽬录;
cd /usr

# 修改vsftp的配置⽂件,禁⽌匿名⽤户登录,同时锁定登录⽤户的⽬录,不能进⾏⽬录切换;
mkdir /var/ftp/test
chmod -R 777 /var/ftp/test
touch /var/ftp/test/1.txt
chown -R jt:jt /var/ftp/test
service vsftpd restart

# 然后把hosts⽂件上传到 a+学号 的⽤户主⽬录中;
put hosts /var/ftp/hosts

# 删除 a+学号 ⽤户信息;
userdel -r a2018666666

实验5

例子1:
在这里插入图片描述

#!/bin/bash
options_show(){
    echo "Use one of the following options:"
    echo "P:To display current directory"
    echo "S:To display the name of running file"
    echo "D:To display today's date and present time(如:2017-04-26 05:45:12) "
    echo "L:To see the list of files in your present working directory"
    echo "W:To see who is logged in"
    echo "I:To see the ip address of this local machine"
    echo "echo \"Q:To quit this program"
    echo "Enter your option and hit:"
}

typeset -l opt
options_show
while read opt
do
    case $opt in
    "p")
        echo `pwd`
    ;;
    "s")
        echo $0
    ;;
    "d")
        echo `date +"%Y-%m-%d %H:%M:%S"`
    ;;
    "l")
        echo `ls .`
    ;;
    "w")
        echo `whoami`
    ;;
    "i")
        echo `ifconfig`
    ;;
    "q")
        break
    ;;
    esac
    options_show
done

例子2
在这里插入图片描述

#!/bin/bash
show(){
    echo "Please input your score:"
}

show
while read score
do
    if [ $score -lt 0 ]
    then
        break
    elif [ $score -lt 60 ]
    then
        echo "Failed"
    elif [ $score -ge 60 -a $score -le 69 ]
    then
        echo "Passed"
    elif [ $score -ge 70 -a $score -le 79 ]
    then
        echo "Medium"
    elif [ $score -ge 80 -a $score -le 89 ]
    then
        echo "Good"
    elif [ $score -ge 90 -a $score -le 100 ]
    then
        echo "Excellent"
    fi
    show
done

例子3
在这里插入图片描述

#!/bin/bash
show(){
    echo "Please input your dir-path:"
}

while read dir
do
    if [ $dir = "q" ]
    then
        break
    fi
    if [ -e $dir ]
    then
        idx=0
        fns=(`ls $dir`) # 这个括号非常重要,可以转成数组
        ls -l $dir | while read x
        do
            if [ $idx == 0 ]
            then
                let idx++
                continue
            else
                echo "/"${fns[$idx-1]}"_"${x:0:1}
                let idx++
            fi
        done
    else
        echo "This path is not exists!"
    fi
    show
done

这里,如果是带有空格的行就需要用read的方式来分割,如果是空格的分割,只需要用数组转换法即可


实验6

例子4
在这里插入图片描述

#!/bin/bash
while read opt
do
    for((i=$opt;i>=1;i--));do
        for((j=$i;j>=1;j--));do
            echo -n "$j " #不换行
        done
        echo
    done
done

例子5

在这里插入图片描述
计划任务:
需求1crontab 30 8 * * * run1.shcrontab 30 23 * * * run2.sh
需求2crontab 30 8-23/1 * * * run3.sh
需求3crontab 50 23 * * * run4.sh

需求1:启动和关闭ftp服务

#!/bin/bash
# run1.sh
systemctl start vsftpd
if [ $?!=0 ];then
	echo "start ftp error" | mail -s "start ftp error" root
else
	pinfo=`ps -ef | grep vsftpd | head -n 1`
	date=`date +"%Y-%m-%d"`
	echo $pinfo>>/var/ftp/${date}.log
fi
#!/bin/bash
# run2.sh
systemctl stop vsftpd
if [ $?!=0 ];then
	echo "Stop ftp error" | mail -s "Stop ftp error" root
fi

需求2:ping Baidu

#!/bin/bash
# run3.sh
date=`date +"%Y-%m-%d"`
ping -c 4 >>/var/ftp/${date}.log

需求3:打包日志

#!/bin/bash
# run4.sh
date=`date +"%Y-%m-%d"`
tar -czvf "/root/${date}.tar.gz" /var/ftp
chmod 400 "/root/${date}.tar.gz"
rm -rf /var/ftp/*

实验7

在这里插入图片描述

main.cpp

#include "my_utils1.hpp"
#include "my_utils2.hpp"
#include <cstdio>

int main()
{
    my_utils1_print();
    my_utils2_print();
    return 0;
}

my_utils1.cpp

#include <cstdio>

void my_utils1_print(){
    printf("This U1\n");
}

my_utils1.hpp

#ifndef _U1_H
#define _U1_H
void my_utils1_print();
#endif

makefile

main : main.cpp my_utils1.cpp my_utils2.cpp my_utils1.hpp my_utils2.hpp
	gcc -o main $^

执行:

make

makefile的其他写法1:

CC=gcc
CFLAGS=-Wall-c   #-Wall:编译后显示所有警告
OBJS=main.cpp my_utils1.cpp my_utils2.cpp my_utils1.hpp my_utils2.hpp

main : ${OBJS}
	${CC} ${OBJS} -o main #需要注意的是前面必须要tab

makefile的其他写法2:

CC=gcc
CFLAGS=-Wall   #-Wall:编译后显示所有警告
OBJS=main.o my_utils1.o my_utils2.o

main : ${OBJS} 
	${CC} ${OBJS} -o main

main.o : main.cpp my_utils1.hpp my_utils2.hpp
	${CC} -c main.cpp

my_utils1.o : my_utils1.cpp my_utils1.hpp
	${CC} -c my_utils1.cpp

my_utils2.o : my_utils2.cpp my_utils2.hpp
	${CC} -c my_utils2.cpp

假如目录结构如下:
在这里插入图片描述

CC=gcc
OBJS=main.o my_utils1.o my_utils2.o

main : ${OBJS} 
	${CC} ${OBJS} -o main

main.o : main.cpp
	${CC} -c main.cpp -If1 -If2

my_utils1.o : f1/my_utils1.cpp f1/my_utils1.hpp
	${CC} -c f1/my_utils1.cpp -If1

my_utils2.o : f2/my_utils2.cpp f2/my_utils2.hpp
	${CC} -c f2/my_utils2.cpp -If2


如果省的记那么多,可以用这个最暴力:

main : main.cpp f1/my_utils1.cpp f2/my_utils2.cpp f1/my_utils1.hpp f2/my_utils2.hpp
	gcc -o main -If1 -If2 $^
  • 25
    点赞
  • 146
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
题目名称 linux实验-基本指令1 题目关键字 linux实验-基本指令1 题目录入时间 2013-4-1 22:36:02 题目内容 1、root帐号登录,查看/tmp目录,如果/tmp目录下没有子目录myshare,则建立该目录。 2、创建帐号testuser。 3、把myshare目录及其目录下的所有文件和子目录的拥有者该为testuser,工作组改为users。 4、切换至testuser帐号。进入/tmp/myshare目录,采用vim编辑器编写以上程序,程序名称为hello.sh: #!/bin/bash echo "app start" echo -e func (){ echo "hello world!" } func echo -e echo "app end" 5、保存hello.sh后,给予hello.sh拥有者可读、可写和可执行的权限,同组可读可执行,其他人可执行权限。 6、输入./hello.sh,观察程序输出的效果。 7、进入testuser的用户主目录,在这个目录下创建hello.sh的软链接,同时拷贝hello.sh到该目录下并改名为hello.sh.bak,要求拷贝时保留文件属性值。 8、退出testuser帐号,回到root帐号,从/开始查找后缀名为.conf的所有文件,把输出结果重定向到testuser帐号的主目录下的output.txt文件。 9、在上一步操作的.conf文件中找出文件容量最大的和最小那个,并把这两个文件的容量大小输出到output.txt文件中。 10、统计出系统中有多少个用户帐号,把数量输出到output.txt文件中。 11、把output.txt文件转换为windows记事本可正规打开的格式。 12、tar打包压缩testuser帐号主目录下的所有文件。 13、用U盘把上一步打包压缩文件拷贝到U盘上。 14、执行userdel -r testuser,执行rm -fr myshare 题目创建人 邝颖杰 题目注释 把打包压缩文件提交即可。
实验三 中规模组合逻辑电路设计(二)(74LS283加法器应用) 1、用两个4位并行加法器和适当的逻辑门电路实现(X+Y)×Z,其中,X=x2x1x0、Y=y2y1y0、Z=z1z0均为二进制。 2、仅使用2片283实现2位十进制数8421码到二进制码的转换。   实验四 中规模组合逻辑电路设计(一)(74LS138译码器和74LS74152/153应用) 1、  用3-8线译码器74LS138和适当的门电路设计三输入一致性检测电路。 2、  用8选1 多路复用器74LS152实现一个全加器。   实验五 触发器应用 1、用J-K触发器设计一个四分频器(即输出频率为输入频率的1/4)。 2、用D触发器设计四人抢答电路。四人参加比赛,每人一个按钮,其中最先按下按钮者,相应的指示灯亮;其他人再按按钮不起作用。 实验六时序逻辑电路设计 1、 设计一个模4可逆计数器。 2、 设计一个0011序列检测器。   实验7 中规模时序逻辑电路设计 1、 用74161设计一个模100计数器。 2、 用74194设计一个00011101序列信号发生器。   综合性、设计性实验 1、设计“串行8421BCD码检验器”,当8421BCD码0000~1001输入时,Z=0,非8421BCD码1010~1111输入时,Z=1;由低位到高位串行输入,每四个二进制数码为一组循环工作。 2、设计一个自动售饮料机的逻辑电路。它的投币口每次只能投入一枚五角或一元的硬币。投入一元五角硬币自动给出一杯饮料;投入两元(两枚一元的硬币)硬币后,再给出饮料的同时找回一枚五角硬币。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值