7月21日上课练习

1、重定向和管道

  • 将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
    tr 'a-z' 'A-Z'</etc/issue >/app/issue.out
  • 将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
    who|tr 'a-z' 'A-Z'>/app/who.out
  • 一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
    Hello, I am 用户名,The system version is here,pleasehelp me to check it ,thanks!
    操作系统版本信息
    ①第一种写法:管道
echo -e "Hello,I am `whoami`,The system version is here ,please help me to check it,thanks! \n `cat /etc/centos-release`"|mail -s "help" root

②第二种写法:多行重定向

Hello,I am root,The system version is here ,please help me to check it,thanks!
 `cat /etc/centos-release`
 end
  • 将/root/下文件列表,显示成一行,并文件名之间用空格隔开
    anaconda-ks.cfg Desktop Documents Downloads install.log install.log.syslog Music Pictures Public Templates Videos
  • 计算1+2+3+..+99+100的总和
    echo {1..100}|tr ' ' '+'|bc
  • 删除Windows文本文件中的‘^M’字符
[root@centos6 app]#cat -A win.txt 
a^M$
b^M$
c[root@centos6 app]#hexdump -c win.txt 
0000000   a  \r  \n   b  \r  \n   c                                    
0000007
[root@centos6 app]#tr -d '\r'<win.txt >win1.txt
[root@centos6 app]#cat -A win1.txt 
a$
b$
  • 处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
[root@centos6 app]#echo “xt.,l 1 jr#!$mn2 c*/fe3 uz4” |tr -dc '0-9 ' 
echo “xt.,l 1 jr#win1.txtmn2 c*/fe3 uz4” |tr -dc '0-9 ' 
 1 12 3 4[root@centos6 app]#
  • 将PATH变量每个目录显示在独立的一行
[root@centos6 app]#echo $PATH|tr ':' '\n'
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/root/bin
  • 将指定文件中0-9分别替代成a-j
[root@centos6 app]#cat f1
123456789
[root@centos6 app]#tr '0-9' 'a-j'<f1
bcdefghij
  • 将文件中每个单词(由字母组成)显示在独立的一行,并无空行
[root@centos6 app]#tr -sc '[:alpha:]' '\n'</etc/issue
CentOS
release
Final
Kernel
r
on
an
m
l
n
t

2、 用户和组管理

  • 创建用户gentoo,附加组为bin和root,默认shell为/bin/csh,注释信息为"Gentoo Distribution"
[root@centos6 ~]#useradd  -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoo
[root@centos6 ~]#getent passwd gentoo
gentoo:x:515:515:Gentoo Distribution:/home/gentoo:/bin/csh
[root@centos6 ~]#id gentoo
uid=515(gentoo) gid=515(gentoo) groups=515(gentoo),0(root),1(bin)
  • 创建下面的用户、组和组成员关系
    名字为admins 的组
    用户natasha,使用admins 作为附属组
    用户harry,也使用admins 作为附属组
    用户sarah,不可交互登录系统,且不是admins 的成员,natasha,harry,sarah密码都是centos
[root@centos6 ~]#groupadd admins
[root@centos6 ~]#useradd -G admins natasha
[root@centos6 ~]#useradd -G admins harry
[root@centos6 ~]#useradd -r -s /sbin/nologin sarah
[root@centos6 app]#vim passwd.txt 
natasha:centos
harry:centos
sarah:centos
[root@centos6 app]#cat passwd.txt |chpasswd
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容