栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

练习Day2

练习Day2

1、取出/etc/inittab文件的第6行。
[root@localhost day2]# sed -n '6p' /etc/inittab
2、取出当前系统上所有用户的shell,要求每种shell只显示一次,并且按顺序进行显示,使用cut、sort结合管道实现 。
[root@localhost day2]# cut -d : -f7 /etc/passwd | sort | uniq -c | sort -n
      1 /bin/sync
      1 /sbin/halt
      1 /sbin/shutdown
      2 /bin/bash
     43 /sbin/nologin
3、如果/var/log/messages文件的行数大于100,就显示好大的文件 。
[root@localhost day2]# vim wcfile.sh
[root@localhost day2]# cat wcfile.sh
#! /bin/bash
NUM=`wc -l /var/log/messages`
if [[ $NUM > 100 ]]
then
        echo "好大的文件"
else
        echo "/var/log/message的行数为:$NUM"
fi
[root@localhost day2]# bash wcfile.sh
好大的文件
4、显示/etc目录下所有以pa开头的文件,并统计其个数 。
[root@localhost day2]# vim pa.sh
[root@localhost day2]# cat pa.sh
#! /bin/bash
echo "/etc目录下所有以pa开头的文件有:"
find /etc/ -name "pa*" -type f
num=`find /etc/ -name "pa*" -type f | wc -l`
echo "文件个数为:$num"
[root@localhost day2]# bash pa.sh
/etc目录下所有以pa开头的文件有:
/etc/security/pam_env.conf
/etc/pam.d/passwd
/etc/passwd-
/etc/passwd
/etc/papersize
/etc/gconf/2/path
/etc/brltty/Input/fs/pacmate.ktb
/etc/brltty/Input/hm/pan.ktb
/etc/brltty/Input/hm/pan.kti
/etc/brltty/Text/pa.ttb
/etc/authselect/password-auth
/etc/mcelog/triggers/page-error-trigger
文件个数为:12
5、如果用户hadoop不存在就添加,否则显示用户已存在 。
[root@localhost day2]# vim user.sh
[root@localhost day2]# cat user.sh
#! /bin/bash
grep hadoop /etc/passwd > /dev/null
if [ $? -ne 0 ]
then
        useradd hadoop
else
        echo "用户已存在"
fi
[root@localhost day2]# bash user.sh
[root@localhost day2]# grep hadoop /etc/passwd
hadoop:x:1001:1001::/home/hadoop:/bin/bash
[root@localhost day2]# bash user.sh
用户已存在
6、编写一个 Shell 程序 test1,程序执行时从键盘读入一个目录名,然后显示这个目录下所有文件的信息 。
[root@localhost day2]# vim test1.sh
[root@localhost day2]# cat test1.sh
#! /bin/bash
read -p "请输入一个目录名:" path
ls -l $path
[root@localhost day2]# bash test1.sh
请输入一个目录名:/scripts/
total 4
drwxr-xr-x. 2 root root 46 Oct 28 00:33 day1
drwxr-xr-x. 2 root root 67 Oct 29 08:20 day2
-rw-r--r--. 1 root root 12 Oct 28 00:33 one.txt
7、编写一个 Shell 程序 test2,从键盘读入 x、y 的值,然后做加法运算,最后输出结果。
[root@localhost day2]# vim test2.sh
[root@localhost day2]# cat test2.sh
#! /bin/bash
read -p "请输入第一个数字:" x
expr $x "+" 10 &>/dev/null
if [ $? -ne 0 ]
then
        echo "请输入正确的数字x"
        exit 1
else
read -p "请输入第二个数字:" y
expr $y "+" 10 &>/dev/null
        if [ $? -eq 0  ]
        then
                sum=$[$x+$y]
                echo "两数相加的和为:$sum"
        else
                echo "请输入正确的数字y"
        fi
fi
[root@localhost day2]# bash test2.sh
请输入第一个数字:a
请输入正确的数字x
[root@localhost day2]# bash test2.sh
请输入第一个数字:1
请输入第二个数字:a
请输入正确的数字y
[root@localhost day2]# bash test2.sh
请输入第一个数字:1
请输入第二个数字:2
两数相加的和为:3
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号