码迷,mamicode.com
首页 > 系统相关 > 详细

Linux基本功杂记——[002]——7月29日课后作业

时间:2016-07-29 22:51:40      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

1、file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和

方法一:
[root@7 fh]# echo $(tr + <file1) | bc 55 方法二:
[root@
7 fh]# x=$(($(tr + <file1))) && echo $x 55 方法三:
[root@
7 fh]# x=$(tr + <file1) | python -c "print($x)" 55

2、处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格

[root@7 fh]# echo xt.,l 1 jr#mn2 c*/fe3 uz4 | grep -oP \d|\s | gawk BEGIN{ORS=""}{print}END{print"\n"}
1 2 3 4

3、将PATH变量每个目录显示在独立的一行

方法一:
[root@7 fh]# echo $PATH | awk BEGIN{RS=":"}{print} /usr/local/bin /usr/bin /usr/local/sbin /usr/sbin /home/fh/.local/bin /home/fh/bin 方法二: [root@7 fh]# echo $PATH | tr ":" "\n" /usr/local/bin /usr/bin /usr/local/sbin /usr/sbin /home/fh/.local/bin /home/fh/bin

4、删除指定文件的空行

sed -i /^$/d file

5、将文件中每个单词(字母)显示在独立的一行,并无空行

[root@7 fh]# cat testfile2 
chen +sljfl s
[root@7 fh]# gawk BEGIN{RS=" "}{print} testfile2 | sed /^$/d
chen
+sljfl
s
[root@7 fh]# grep -oP "." testfile2 | sed /^ *$/d
c
h
e
n
+
s
l
j
f
l
s

6、创建用户gentoo,附加组为bin和root,默认shell为/bin/csh,注释信息为"Gentoo Distribution"

[root@7 fh]# useradd -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoo
[root@7 fh]# grep gentoo /etc/passwd
gentoo:x:1002:1002:Gentoo Distribution:/home/gentoo:/bin/csh

7、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中 

方法一:
[root@7 fh]# tr [a-z] [A-Z] < /etc/issue > /tmp/issue.out && cat /tmp/issue.out
\S
KERNEL \R ON AN \M
方法二:
[root@7 fh]# sed s/[a-z]/\u&/g /etc/issue > /tmp/issue.out && cat /tmp/issue.out
\S
KERNEL \R ON AN \M

 问题扩展:将/etc/issue文件中的每个单词的首(末)字母转换为大写(或小写)后保存至/tmp/issue.out文件中

首字母转大写:
[root@7 fh]# sed s/\b[a-z]/\u&/g /etc/issue > /tmp/issue.out && cat /tmp/issue.out
\S
Kernel \R On An \M
首字母转小写:
[root@7 fh]# sed s/\b[A-Z]/\l&/g /etc/issue > /tmp/issue.out && cat /tmp/issue.out
\s
kernel \r on an \m
末字母转小写:
[root@7 fh]# sed s/[A-Z]\b/\l&/g /etc/issue > /tmp/issue.out && cat /tmp/issue.out
\s
Kernel \r on an \m

8、将当前系统登录用户的信息转换为小写 

[root@7 ~]# w | tr [A-Z] [a-z]
 06:15:58 up  2:39,  2 users,  load average: 0.00, 0.01, 0.02
user     tty      from             login@   idle   jcpu   pcpu what
fh       pts/0    172.18.21.244    05:16    3:10   0.15s  0.05s sshd: fh [priv]     
root     pts/1    172.18.21.244    06:12    6.00s  0.00s  0.00s w
[root@7 ~]# w | sed s/[A-Z]/\l&/g
 06:16:15 up  2:39,  2 users,  load average: 0.00, 0.01, 0.02
user     tty      from             login@   idle   jcpu   pcpu what
fh       pts/0    172.18.21.244    05:16    3:27   0.15s  0.05s sshd: fh [priv]     
root     pts/1    172.18.21.244    06:12    7.00s  0.00s  0.00s w

9、将/root/下文件列表,显示成一行,并文件名之间用空格隔开

[root@7 ~]# ls -a | gawk BEGIN{ORS=" "}{print}
. .. .bash_history .bash_logout .bash_profile .bashrc .cshrc .tcshrc anaconda-ks.cfg tmpfile
[root@7 ~]# ls -a | tr \n  
. .. .bash_history .bash_logout .bash_profile .bashrc .cshrc .tcshrc anaconda-ks.cfg tmpfile

 10、创建下面的用户、组和组成员关系

名字为admins 的组

[root@7 fh]# groupadd admins
[root@7 fh]# grep admins /etc/group
admins:x:1003:

用户natasha,使用admins 作为附属组

[root@7 fh]# useradd -G admins natasha
[root@7 fh]# groups  natasha
natasha : natasha admins

用户harry,也使用admins 作为附属组

[root@7 fh]# useradd -G admins harry
[root@7 fh]# groups  harry
natasha : harry admins

用户sarah,不可交互登录系统,且不是admins 的成员,natasha,harry,sarah密码都是centos

[root@7 fh]# useradd -s /sbin/nologin sarah
[root@7 fh]# groups sarah
sarah : sarah
[root@7 fh]# echo ‘centos‘ | passwd --stdin natasha
Changing password for user natasha.
passwd: all authentication tokens updated successfully.
[root@7 fh]# echo ‘centos‘ | passwd --stdin harry
Changing password for user harry.
passwd: all authentication tokens updated successfully.
[root@7 fh]# echo ‘centos‘ | passwd --stdin sarah
Changing password for user sarah.
passwd: all authentication tokens updated successfully.

Linux基本功杂记——[002]——7月29日课后作业

标签:

原文地址:http://www.cnblogs.com/hadex/p/5719762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!