有关shell指令练习2

写一个shell脚本,将以下内容放到脚本中

在家目录下创建目录文件,dir

dir下创建dir1和dir2

把当前目录下的所有文件拷贝到dir1中,

把当前目录下的所有脚本文件拷贝到dir2中

把dir2打包并压缩为dir2.tar.xz

再把dir2.tar.xz移动到dir1中

解压dir1中的压缩包

bash 复制代码
#!/bin/bash    

#a-在家目录下创建目录文件,dir
mkdir -p ~/dir

#b-在dir下创建dir1和dir2
mkdir -p ~/dir/dir1
mkdir -p ~/dir/dir2

#c-把当前目录下的所有文件拷贝到dir1中
cp * ~/dir/dir1

#d-把当前目录下的所有脚本文件拷贝到dir2中
cp *.sh ~/dir/dir2

#e-把dir2打包并亚索为dir2.tar.xz
tar -cJf ~/dir/dir2.tar.xz -C ~/dir dir2

#f-再把dir2.tar.xz移动到dir1中
mv ~/dir/dir2.tar.xz ~/dir/dir1/

#g-解压dir1中的压缩包
tar -xf ~/dir/dir/dir2.tax.xz -C ~/dir/dir1

写一个脚本,包含以下内容:显示/etc/group文件中第五行的内容

创建目录/home/ubuntu/copy

切换工作路径到此目录

复制/etc/shadow到此目录,并重命名为test

将当前目录中test的所属用户改为root

将test中其他用户的权限改为没有任何权限

bash 复制代码
#!/bin/bash    

#写一个脚本,包含以下内容:显示/etc/group文件中第五行的内容
head -5 /etc/group | tail -1

#创建目录/home/ubuntu/copy
mkdir -p /home/ubuntu/copy

#切换工作路径到此目录
cd /home/ubuntu/copy

#复制/etc/shadow到此目录,并重命名为test
cp /etc/shadow .
mv shadow test
#将当前目录中test的所属用户改为root
chown root test

#将test中其他用户的权限改为没有任何权限
chmod o-rwx,g-rwx test

已知网址www.hqyj.com,使用expr截取出www、hqyj、com

bash 复制代码
#!/bin/bash

str="www.hqyj.com"
expr substr "$str" 1 3
expr substr "$str" 5 4
expr substr "$str" 10 12

终端输入数组终端元素并输出,并且输出数组中元素的个数

bash 复制代码
     1	#!/bin/bash
     2	
     3	arr=("$@")
     4	count1=${arr[*]}
     5	echo "终端输入数组终端元素:"$count1
     6	count=${#arr[@]}
     7	echo "终端输入的元素个数:"$count
相关推荐
虚伪的空想家7 小时前
ip网段扫描机器shell脚本
android·linux·网络协议·tcp/ip·shell·脚本·network
谅望者1 天前
Linux文件查看命令完全指南:cat、less、head、tail、grep使用详解
linux·excel·less·shell·文件操作·命令行·系统运维
hello_2503 天前
Shell脚本高效编写技巧
运维·shell
leaf9z4 天前
阿里云ECS在线扩容磁盘
阿里云·云计算·shell
gfdgd xi6 天前
deepin 终端,但是版本是 deepin 15 的
linux·python·架构·ssh·bash·shell·deepin
大米粥哥哥10 天前
Qt QProcess基于Linux的命令管道符号无效问题【已解决】
linux·qt·shell·qprocess·1024程序员节·管道符号
缘友一世16 天前
深入理解Shell与反弹Shell:从原理到实战
linux·网络安全·shell
hour_go18 天前
Linux Shell 实验一:数据采集与脚本调试错误总结
linux·笔记·bash·shell·实验报告·错误排查
struggle202519 天前
AxonHub 开源程序是一个现代 AI 网关系统,提供统一的 OpenAI、Anthropic 和 AI SDK 兼容 API
css·人工智能·typescript·go·shell·powershell
shark20 天前
无需放弃变更、关闭占用程序!用暂存区和 git底层命令实现 Git 变更备份
git·shell·自动化运维