编写shell脚本完成如下功能:(1)显示当前登录用户名;(2)显示最近10条命令历史;(3)打印当前日期;(4)退出脚本,并置退出码为5
时间: 2023-10-10 18:11:05 浏览: 172
可以编写一个名为 `my_script.sh` 的 shell 脚本,内容如下:
```bash
#!/bin/bash
# 显示当前登录用户名
echo "当前登录用户名:$USER"
# 显示最近10条命令历史
echo "最近10条命令历史:"
history 10
# 打印当前日期
echo "当前日期:$(date)"
# 退出脚本,并置退出码为5
exit 5
```
这个脚本中,第一行 `#!/bin/bash` 表示使用 bash shell 来解释执行该脚本。接下来的每一行都是一个命令或语句,用于完成相应的功能。最后一行 `exit 5` 表示退出脚本,并将退出码设置为 5。
要执行该脚本,需要给它执行权限,可以使用 `chmod +x my_script.sh` 命令来赋予执行权限,然后使用 `./my_script.sh` 命令来执行。
相关问题
1、编写shell脚本完成如下功能:.(1)显示当前登录用户名;(2)显示最近10条命令历史;(3)打印当前日期; (4)退出脚本,并置退出码为5 2、将当前目录文件msg中的内容发给当前所有的登录用户。 3.编程完成如下功能:使用 10系统调用判断指定目录中的所有文件类型,并统计各种文件类型的个 注:指定目录在程序执行时通过键盘读入;不需要写 include 涉及到的系统调用和函数有: int fstat(int fd, struct stat *buf) int stat (const char *path, struct stat *buf) int Istat (const char *path, struct stat *buf) DIR opendir (char *name) struct dirent * readdir(DIR *dir) S_ISREG、 S_ISDIR、 S_ISBLK、S_ISCHR、 S_ISSOCK、S_ISFIFO、S_ISLNK
1. 以下是实现这个shell脚本的示例代码:
```bash
#!/bin/bash
# 显示当前登录用户名
echo "当前登录用户名为: $USER"
# 显示最近10条命令历史
echo "最近10条命令历史为:"
history | tail -n 10
# 打印当前日期
echo "当前日期为: $(date)"
# 退出脚本,并置退出码为5
exit 5
```
2. 以下是将当前目录文件msg中的内容发给当前所有的登录用户的示例代码:
```bash
#!/bin/bash
# 获取当前所有登录用户的用户名
users=$(who | awk '{print $1}' | sort | uniq)
# 遍历所有用户,将msg文件的内容发送给他们
for user in $users; do
if [ -n "$(who | grep $user)" ]; then
cat msg | write $user
fi
done
```
3. 以下是使用系统调用完成指定目录中的所有文件类型统计的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
int main() {
char path[100];
printf("请输入指定目录路径: ");
scanf("%s", path);
DIR *dir;
struct dirent *entry;
struct stat statbuf;
int count_reg = 0, count_dir = 0, count_blk = 0, count_chr = 0, count_sock = 0, count_fifo = 0, count_lnk = 0;
if ((dir = opendir(path)) == NULL) {
perror("opendir error");
exit(1);
}
while ((entry = readdir(dir)) != NULL) {
char filename[100];
sprintf(filename, "%s/%s", path, entry->d_name);
if (lstat(filename, &statbuf) == -1) {
perror("lstat error");
exit(1);
}
if (S_ISREG(statbuf.st_mode)) {
count_reg++;
} else if (S_ISDIR(statbuf.st_mode)) {
count_dir++;
} else if (S_ISBLK(statbuf.st_mode)) {
count_blk++;
} else if (S_ISCHR(statbuf.st_mode)) {
count_chr++;
} else if (S_ISSOCK(statbuf.st_mode)) {
count_sock++;
} else if (S_ISFIFO(statbuf.st_mode)) {
count_fifo++;
} else if (S_ISLNK(statbuf.st_mode)) {
count_lnk++;
}
}
printf("普通文件个数:%d\n", count_reg);
printf("目录个数:%d\n", count_dir);
printf("块设备个数:%d\n", count_blk);
printf("字符设备个数:%d\n", count_chr);
printf("套接字个数:%d\n", count_sock);
printf("FIFO/pipe个数:%d\n", count_fifo);
printf("符号链接个数:%d\n", count_lnk);
closedir(dir);
return 0;
}
```
编写Shell脚本,实现批量添加用户功能,要求如下: 1)用户名格式统一,为相同字符串加数字编号,如qwe1-qwe9 2)用户密码与用户名相同
好的,以下是一个简单的 Shell 脚本来实现批量添加用户的功能:
```bash
#!/bin/bash
read -p "请输入班级人数: " num
# 定义用户名前缀和密码
username_prefix="qwe"
password_prefix="qwe"
# 循环添加用户
for i in $(seq 1 $num)
do
# 构造用户名和密码
username="$username_prefix$i"
password="$password_prefix$i"
# 添加用户
useradd "$username" -m -s /bin/bash
echo "$username:$password" | chpasswd
# 输出添加成功的用户信息
echo "添加用户成功: 用户名:$username 密码:$password"
done
# 将用户信息保存到文件中
cat /etc/passwd | grep "$username_prefix" > users.txt
```
在这个脚本中,我们首先读取输入的班级人数,然后定义了用户名前缀和密码前缀。接下来,我们使用了一个 for 循环来批量添加用户。在每次循环中,我们构造了用户名和密码,然后使用 useradd 命令添加用户,并使用 chpasswd 命令设置用户密码。最后,我们输出了添加成功的用户信息,并将所有用户名以指定前缀的用户信息保存到文件中。
请注意,由于这是一个 Shell 脚本,因此需要在 Linux 或 macOS 系统中运行。