Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
muzixiaoyao committed Jan 24, 2019
1 parent 6597d67 commit 3dc1960
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 178 deletions.
117 changes: 117 additions & 0 deletions 系统-2019-1/脚本练习题答案_2019_1_23.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# 脚本练习题:

1. 写一个脚本,将文件(arg1)复制到目的地(arg2)

```bash
#!/bin/bash
cp $1 $2
```

2. 输出传入的参数的个数,将传入的参数输出到屏幕上,用循环的方式

```bash
#!/bin/bash
echo "脚本有$#个参数"
for arg in $*
do
echo "$arg"
done
```

3. 输出参数一文件的第一行和最后一行内容,并且要输出文件的总行数和总单词数字,输入结果可以类似下面这样:

```bash
#!/bin/bash
echo "filename: $1"
echo "homeline:`head -1 $1`"
echo "endline: `tail -n 1 $1`"
echo "lines: `wc -l < $1`"
echo "words: `wc -w < $1`"
```

4. 检查文件是否存在,并且输出结果,如果存在,则将文件的类型输出在屏幕上

```bash
#!/bin/bash
if [ -e $1 ]
then
echo "文件 $1 存在"
echo "文件类型为`file $1`"
else
echo "文件不存在"
fi
```

5. 输出文件中开头为"eagle "的行的第二列

```bash
#!/bin/bash
cat $1 | grep "^eagle " | awk '{print $2}'
```

6. 判断指定文件权限是否为755,如果不是则将权限改为755

```bash
#!/bin/bash
qx=`ls -al $1 | cut -d" " -f1`

if [ $qx = "-rwxr-xr-x." ]
then
echo "文件权限合格"
else
chmod 755 $1
echo "文件权限不合格,已为您自动修改为正确权限。"
fi
```

7. 删除系统中除了当前登陆中的用户之外的其他普通用户(id>1000),删干净

```bash
#!/bin/bash
dluser=`who | awk '{print $1}'`
while read line
do
userid=`echo $line | awk -F: '{print int($3)}'`
if [ $userid -ge 1000 ]
then
echo $userid
username=`echo $line | awk -F: '{print $1}'`
echo $username
isuser=0
for i in $dluser
do
if [ $i = $username ]
then
isuser=1
fi
done
if [ $isuser -eq 0 ]
then
echo "发现id大于1000没有登录的用户$username"
#userdel -r $username
fi
fi
done < /etc/passwd
```

8. 测试目标站点是否能访问,如果可以,则将目标站点curl http访问到内容的第8行追加到当前目录下的test.txt文件中,追加内容格式示例

http://blog.xiaoyaozone.com---curl的内容

```bash
#!/bin/bash
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
if [ $? -eq 0 ]
then
curl $1 > temp.txt
re=`cat temp.txt | sed -n "8p"`
if [ re != "" ]
then
echo $re >> test.txt
else
echo "no line 8"
fi
else
echo "Host $1 is Off-line."
fi
```
Loading

0 comments on commit 3dc1960

Please sign in to comment.