相关练习:
1、file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和
echo $[1+2+3+4+5+6+7+8+9+10]
echo "1+2+3+4+5+6+7+8+9+10" | bc
电脑演示
[[email protected] Desktop]# echo $[1+2+3+4+5+6+7+8+9+10]
55
[[email protected] Desktop]# echo "1+2+3+4+5+6+7+8+9+10" | bc
55
[[email protected] Desktop]#
2、处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
echo "xt.,l 1 jr#!$mn2 c*/fe3 uz4" | tr -d "xt.,ljr#!$mnc*/feuz"
电脑演示
[[email protected] Desktop]# echo "xt.,l 1 jr#!$mn2 c*/fe3 uz4" | tr -d "xt.,ljr#!$mnc*/feuz"
echo "xt.,l 1 jr#bcmn2 c*/fe3 uz4" | tr -d "xt.,ljr#bcmnc*/feuz"
1 2 3 4
[[email protected] Desktop]#
3、将PATH变量每个目录显示在独立的一行
echo $PATH | tr ":" "\n"
电脑演示
[[email protected] Desktop]# echo $PATH | tr ":" "\n"
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/sbin
/sbin
/usr/local/bin
/usr/bin
/bin
/root/bin
[[email protected] Desktop]#
4、删除指定文件的空行
cat /root/install.log | tr -s "\n"【利用-s的循环】
电脑演示
[[email protected] Desktop]# cat /root/install.log | tr -s "\n"
{
333
anaconda-ks.cfg
Desktop
Documents
Downloads
f1
f22
f99
f[a,b,c,d,e,f]
history.log
install.log
install.log.syslog
Music
nusers
Pictures
Public
Templates
Videos
[[email protected] Desktop]#
5、将文件中每个单词(字母)显示在独立的一行,并无空行
cat /root/install.log | tr " " "\n" | tr -s "\n"
电脑演示
[[email protected] Desktop]# cat /root/install.log | tr " " "\n" | tr -s "\n"
{
333
anaconda-ks.cfg
Desktop
Documents
Downloads
f1
f22
f99
f[a,b,c,d,e,f]
history.log
install.log
install.log.syslog
Music
nusers
Pictures
Public
Templates
Videos
6、创建用户tiantian,附加组为bin和root,默认shell为/bin/csh,注释信息为"Tiantian Distribution"
useradd -G bin,root -s /bin/csh -c "Tiantian Distribution" tiantian
电脑演示
[[email protected] Desktop]# useradd -G bin,root -s /bin/csh -c "Tiantian Distribution" tiantian
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianya:x:500:500:tianya:/home/tianya:/bin/bash
gentoo:x:501:501:Gentoo Disttibuution:/home/gentoo:/bin/csh
tian:x:502:502::/home/tian:/bin/bash
tiantian:x:503:503:Tiantian Distribution:/home/tiantian:/bin/csh
[[email protected] Desktop]# ^C
[[email protected] Desktop]#
7、创建下面的用户、组和组成员关系
名字为admins 的组
用户natasha,使用admins 作为附属组
用户harry,也使用admins 作为附属组
用户sarah,不可交互登录系统,且不是admins 的成员,natasha,harry,sarah密码都是centos
1 #!/bin/bash
2 groupadd admins
3 useradd -G admins natasha
4 useradd -G admins harry
5 useradd -s /sbin/nologoin sarah
6 echo "centos" | passwd --stdin natasha
7 echo "centos" | passwd --stdin harry
8 echo "centos" | passwd --stdin sarah
电脑演示
[[email protected] Desktop]# nano f1.xt
[[email protected] Desktop]# cat f1.xt
#!/bin/bash
groupadd admins
useradd -G admins natasha
useradd -G admins harry
useradd -s /sbin/nologoin sarah
echo "centos" | passwd --stdin natasha
echo "centos" | passwd --stdin harry
echo "centos" | passwd --stdin