答案:
#!/bin/bash
file_path="path/to/your/file" # 替换为你想要判断的文件路径
if [ -c "$file_path" ]; then
echo "文件是字符设备文件"
cp "$file_path" /dev/
else
echo "文件不是字符设备文件"
fi
答案:
#!/bin/bash
group_name="class1"
start_index=1
end_index=30
# 添加新组
sudo groupadd "$group_name"
# 添加属于该组的用户
for ((i=start_index; i<=end_index; i++))
do
username="std$(printf "%02d" $i)" # 格式化用户名为 stdxx,其中 xx 为两位数字
sudo useradd -m -s /bin/bash -G "$group_name" "$username"
echo "添加用户 $username 成功"
done
答案:
#!/bin/bash
start_index=1
end_index=50
# 删除指定用户
for ((i=start_index; i<=end_index; i++))
do
username="stud$i"
sudo userdel -r "$username" # -r 参数表示同时删除用户的家目录
echo "已删除用户 $username"
done
答案:
#!/bin/bash
# 定义备份目录和目标目录
backup_dir="/user/backup"
target_dir="/tmp"
# 获取当前时间和星期几
current_time=$(date "+%Y-%m-%d %H:%M:%S")
current_day=$(date "+%u")
# 判断是否为周日(星期天)
if [ "$current_day" -eq 7 ]; then
# 执行备份操作
cp -r "$backup_dir" "$target_dir"
echo "[$current_time] 备份成功!"
else
echo "[$current_time] 今天不是周日,不执行备份操作。"
fi
答案:
#i/bin/bash
#三个数,两两比较一次就可以了
t=0
read a
read b
read c
if [ $a -gt $b ];then
t=$a
a=$b
b=$t
fi
if [ $a -gt $c ];then
t=$a
a=$c
c=$t
fi
if [ $b -gt $c ];then
t=$b
b=$c
c=$t
fi
echo "$a $b $c"
答案:
#!/bin/bash
num=0
for i in $(ls)
do
num=$[$num+1]
echo "item:$i"
done
echo "the number of files is $num"
答案:
#!/bin/bash
for FileName in `ls -l | awk '$5>10240 {print $9}'`
do
mv $FileName /tmp
done
答案:
#!/bin/bash
read -p “请输入字符:” char
echo $char|rev
答案:
#!/bin/bash
for i in `seq -w 10`
do
useradd CSDN$i >/dev/null
#取8个字符串
passwd=`cat /dev/urandom |head |md5sum | head -c8`
#账户密码存放
echo CSDN$i $passwd >> /CSDN.txt
echo $passwd | passwd --stdin CSDN$i >/dev/null
if [ $? -eq 0 ];then
echo 用户CSDN${i}创建成功
else
echo 用户CSDN${i}创建失败
fi
done
答案:
#!/bin/bash
result=0
read -p "请输入第一个数:" a
read -p "请输入第二个数:" b
read -p "请输入运算符号:" c
if [ $c = "+" ];then
result=$(($a+$b))
echo " $a $c $b = $result "
elif [ $c ="-" ];then
result=$(($a-$b))
echo " $a $c $b = $result "
elif [ $c = "*" ];then
result=$(($a*$b))
echo " $a $c $b = $result "
elif [ $c = "/" ];then
result=$(($a/$b))
echo " $a $c $b = $result "
fi