bash编程练习题及答案

1、 删除/etc/grub.conf文件中行首的空白符

sed [email protected]^[[:space:]]*@@‘ /etc/grub.conf

2、 替换/etc/inittab文件中"id:3:initdefault:" 一行中的数字为5

3、 删除/etc/inittab文件中的空白行

sed /^$/d /etc/inittab

4、 删除/etc/inittab文件中开头的#号

sed ‘s/^#*//g‘ /etc/inittab

5、 删除某文件中开头的#号及后面的空白字符,但要求#号后面必须有空白字符

sed s/^#[[:space:]]*//g 123.txt

6、 删除某文件中以空白字符后面跟#类的行中的开头的空白字符及#

#abc

# hello world

#hi world

sed s/^[[:space:]]*#//g 123.txt

7、 取出一个文件路径的目录名称

/etc/fstab

/var/log

取出etc 和var

目录名:

echo "/etc/rc.d/" | sed -r ‘[email protected]^(/.*/)[^/]+/[email protected]\[email protected]‘

基名:

echo "/etc/rc.d/" | sed -r ‘[email protected]^/.*/([^/]+)/[email protected]\[email protected]‘

老师标准答案:

sed练习:
1、删除/etc/grub.conf文件中行首的空白符;
sed -r ‘[email protected]^[[:spapce:]][email protected]@g‘ /etc/grub.conf
2、替换/etc/inittab文件中"id:3:initdefault:"一行中的数字为5;
sed ‘[email protected]\(id:\)[0-9]\(:initdefault:\)@\15\[email protected]‘ /etc/inittab
3、删除/etc/inittab文件中的空白行;
sed ‘/^$/d‘ /etc/inittab
4、删除/etc/inittab文件中开头的#号;
sed ‘[email protected]^#@@g‘ /etc/inittab
5、删除某文件中开头的#号及后面的空白字符,但要求#号后面必须有空白字符;
sed -r ‘[email protected]^#[[:space:]][email protected]@g‘ /etc/inittab
6、删除某文件中以空白字符后面跟#类的行中的开头的空白字符及#
sed -r ‘[email protected]^[[:space:]]+#@@g‘ /etc/inittab
7、取出一个文件路径的目录名称;
echo "/etc/rc.d/" | sed -r ‘[email protected]^(/.*/)[^/]+/[email protected]\[email protected]‘    
基名:
echo "/etc/rc.d/" | sed -r ‘[email protected]^/.*/([^/]+)/[email protected]\[email protected]‘

#abc
# hello world
   # hi world

练习:
传递一个用户名参数给脚本,判断此用户的用户名跟其基本组的组名是否一致,并将结果显示出来。
#!/bin/bash
#

if ! id $1 &> /dev/null; then

echo "No such user"

exit 10

fi
if [ `id -n -u $1` == `id -n -g $1` ]; then
  echo "The same"
else
  echo "Not the same"
fi

=================================================================

练习:写一个脚本

传递一个参数(单字符就行)给脚本,如参数为q、quit、Q、Quit,就退出脚本,否则, 就显示用户的参数:

#!/bin/bash
#
if [ $1 = ‘q‘ ];then
  echo "Quitting"
  exit 0
elif [ $1 = ‘Q‘];then
  echo "Quiting"
  exit 1
elif [ $1 = ‘Quit‘ ]; then
echo "Quitting"
  exit 2
elif [ $1 = ‘QUIT‘ ]; then
  echo "quiting"
  exit 3
else
  echo "$1"
fi

=======================================================================

练习:
传递三个参数给脚本,第一个为整数,第二个为算术运算符,第三个为整数,将计算结果显示出来,要求保留两位精度。形如:
./calc.sh 5 / 2

提示:

echo "scale=2;111/22;" | bc

bc <<< "scale=2;111/22"

#!/bin/bash
#
A=$1
x=$2
B=$3
result= echo "scale=2;$A$x$B;"|bc
echo $result

====================================================================

写一个脚本:
判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor id一行中。
如果其生产商为AuthenticAMD,就显示其为AMD公司;
如果其生产商为GenuineIntel,就显示其为Intel公司;
否则,就说其为非主流公司;

#!/bin/bash
#
mycpu=`cat /proc/cpuinfo | grep vendor | cut -d: -f2 | head -1 | sed ‘s/^[[:space:]]//g‘`
if [ $mycpu = "GenuineIntel" ]; then
  echo "INTEL chip inside"
elif [ $mycpu = "GenuineIntel" ]; then
  echo "AMD chip inside"
else
  echo "unkown cpu type"
fi

=====================================================================

写一个脚本:
给脚本传递三个整数,判断其中的最大数和最小数,并显示出来。
MAX=0
MAX -eq $1
MAX=$1
MAX -lt $2
MAX=$2

用两两比较法

#!/bin/bash
#
MAX=0
MIN=0
if [ $1 -lt $2 ]; then
  MAX=$2
  MIN=$1
else
  MAX=$1
  MIN=$2
fi
if [ $MAX -lt $3 ]; then
  MAX=$3
else
  MIN=$3
fi
echo "Max number is $MAX, the min number is $MIN"

======================================================================

练习:
传递3个参数给脚本,参数均为用户名。将此些用户的帐号信息提取出来后放置于/tmp/testusers.txt文件中,并要求每一行行首有行号。

echo "1 $LINES" >> / /tmp/testusers

echo "2 $LINES" >> / tmp/testusers

####脚本有问题###

!/bin/bash
#
for I in `seq 1 $#`;do
  #echo "`cat /etc/passwd | grep $I`" >> /tmp/tempusers
  echo $`$I`
done

=======================================================================

循环练习

向系统内所有用户问好:

LINES=`wc -l /etc/passwd | cut -d‘ ‘ -f1`

for I in `seq 1 $LINES`;do echo "Hello, `head -n $I /etc/passwd| tail -1 | cut -d: -f1`";done

======================================================================

写一个脚本:
1、设定变量FILE的值为/etc/passwd
2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如: 
     Hello, root, your shell: /bin/bash
3、统计一共有多少个用户

扩展: 只向默认shell为bash的用户问好

#!/bin/bash
#
FILE=/etc/passwd
LINES=`wc -l /etc/passwd| cut -d‘ ‘ -f1`
for I in `seq 1 $LINES`;do
  echo "Hello,`cat /etc/passwd | head -n $I | tail -1 | cut -d: -f1 ` , Your shell is `cat /etc/passwd| head -n $I | tail -1 | cut -d: -f7`"
done

扩展: 只向默认shell为bash的用户问好,

扩展版:

#!/bin/bash
#
SUM=0
FILE=/etc/passwd
LINES=`wc -l /etc/passwd| cut -d‘ ‘ -f1`
for I in `seq 1 $LINES`;do
  USER=`cat /etc/passwd | head -n $I | tail -1 | cut -d: -f1 `
  USHELL=`cat /etc/passwd| head -n $I | tail -1 | cut -d: -f7`
  if [[ $USHELL == ‘/bin/bash‘ ]]; then       #if $USHELL is null, then you     will get error with the     sinlge[], so please use     double[[]] when     judge the string equation.
  echo "Hello, $USER , Your shell is $USHELL"
  SUM=$[$SUM+1]
  fi
done
echo "Total user is $SUM"

=======================================================================

写一个脚本:
1、添加10个用户user1到user10,密码同用户名;但要求只有用户不存在的情况下才能添加;

#!/bin/bash
#
for I in `seq 1 10`; do
  if ! id user$I &> /dev/null; then
    useradd user$I &> /dev/null
    echo "user$I"| passwd --stdin user$I &> /dev/nul
  else
    echo "user$I already existed, will not create"
  fi
done

扩展:
接受一个参数:
add: 添加用户user1..user10
del: 删除用户user1..user10
其它:退出

#!/bin/bash
#
if [[ $1 == add ]]; then
  for I in `seq 1 10`; do
    if ! id user$I &> /dev/null; then
      useradd user$I &> /dev/null
      echo "user$I"| passwd --stdin user$I &> /dev/nul
    else
      echo "user$I already existed, will not create"
    fi
  done
elif [[ $1 == del ]];then
  echo "delete function is running"
  for I in `seq 1 10`;do
  userdel -rf user$I &> /dev/null
  done
fi

====================================================================

写一个脚本:
计算100以内所有能被3整除的正整数的和;
取模,取余:%
3%2=1
100%55=45

#!/bin/bash
#
SUM=0
for I in `seq 1 100`;do
  if [ $(($I%3)) = 0 ];then
    SUM=$[$SUM+$I]
  fi
done
echo "The sum is $SUM"

====================================================================

写一个脚本:
计算100以内所有奇数的和以及所有偶数的和;分别显示之;

#!/bin/bash
#
J=0
O=0
for I in `seq 1 100`;do
  if [ $(($I%2)) = 0 ]; then
    J=$[$J+$I]
  else
    O=$[$O+$I]
  fi
done
echo "SUM J is $J"
echo "SU O is $O"

=====================================================================

写一个脚本,分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数。显示结果形如:
BASH,3users,they are:
root,redhat,gentoo

NOLOGIN, 2users, they are:
bin,ftp

可以追加到一个文件内,然后在 显示那个文件

#!/bin/bash
#
ushells=""
bash_count=0
nologin_count=0
LINES=`cat /etc/passwd | wc -l`
for I in `seq 1 $LINES`;do
  if [[ `cat /etc/passwd | head -n $I | tail -1 | cut -d: -f7` == /bin/bash ]]; then
   echo " `cat /etc/passwd | head -n $I | tail -1 | cut -d: -f1`" >> /tmp/bash_users
   bash_count=$[$bash_count+1]
  elif [[ `cat /etc/passwd | head -n $I | tail -1 | cut -d: -f7` == /sbin/nologin ]]; then 
    echo " `cat /etc/passwd | head -n $I | tail -1 | cut -d: -f1`" >> /tmp/nologin_users
    nologin=$[$nologin+1]
  fi
done
echo "BASH, $bash_countusers, they are:"
echo `cat /tmp/bash_users`
echo "NOLOGIN, $nologin_countusers,they are:"
echo `cat /tmp/nologin_users`

=======================================================================

时间: 2024-07-28 23:00:36

bash编程练习题及答案的相关文章

6、50道JAVA基础编程练习题跟答案

1 50道JAVA基础编程练习题 2 [程序1] 3 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 4 程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... 5 public class Prog1{ 6 public static void main(String[] args){ 7 int n = 10; 8 System.out.println("第"+n+

bash编程:Shell练习题

bash编程:练习题 1. 写一个脚本:如果某路径不存在,则将其创建为目录:否则显示其存在,并显示内容类型 #!/bin/bash # baseurl=/var/tmp/testdir if [ -e $baseurl ]; then echo "file is no exists." else mkdir -p $baseurl file $baseurl fi 2. 写一个脚本,完成如下功能:判断给定的两个数值,孰大孰小:给定数值的方法:脚本参数,命令交互 #!/bin/bash

Python编程快速上手-让繁琐工作自动化 第四章 列表练习题及其答案

第四章 列表练习题及其答案 1.什么是[]? 答:空的列表值,它是一个列表,不包含任何列表项.这类似于''是空的字符串值. 2.如何将'hello'赋值给列表的第三个值,而列表保存在名为spam的变量中?(假设变量包含[2, 4, 6, 8, 10]) 答:spam[2] = 'hello'(注意列表中的第3个值下标是2,因为第一个值下标是0.) 对接下来的3个问题,假定spam包含列表['a', 'b', 'c', 'd'] 3.spam[int('3'*2)/11]求值为多少? 答:'d'(

bash编程快速入门

首先,我们简单的介绍一下bash,bash是GNU计划编写的Unixshell,它是许多Linux平台上的内定shell,它提供了用户与系统的很好的交互,对于系统运维人员,bash的地位是举足轻重的,bash编程能很快处理日常的任务 bash入门,一个最简单的bash例子 #vim hello.sh #!/bin/bash #This is the first example of the bash #echo "Hello world" 下面,我们就这个简单的bash 脚本来介绍一下

Linux bash编程入门

一.bash编程入门 编程语言: 编译型语言:编译器  c,c++ 解释型语言:解释器 解释器可独立运行 变量:保持数据的载体,命令的内存空间 本地变量: 环境变量 局部变量: 位置参数变量:$1,$2..., 特殊变量: $0 当前脚本的名字 shell脚本:shebang #!/bin/bash         #解释此脚本的shell路径,内核调用对应的解释器来解释脚本 #Description: #Version: #Author: #License: #Datetime: 脚本文件,其

bash编程初体验之for

bash编程初体验之for for while until 概述 本文将介绍以for为代表的循环语句在shell 脚本中的应用,常见的循环语句有for, while, until,作为循环语句,顾名思义,它就是重复地做一件事,直到满足某一条件而退出:另外,还有两个循环控制语句continue与break来配合循环语句,以实现临时中断或跳出循环的功能:以下为for, while, until的知识点提炼: for, while, until 进入条件          for: 列表元素非空   

bash 编程 awk

awk提供了一个类编程环境,允许修改和重新组织文件中的数据        定义变量来保存数据        使用算术和字符串操作符来处理数据        使用结构化编程概念        提取数据文件中的数据并将它们按另一顺序提取数据元素重新放置,从而生成格式化报告 命令格式 awk options program file 选项 -F fs 指定分隔符 -f file 指定读取程序的文件名 -v var=vlaue 定义程序中的一个变量及其默认值 -mf n 要处理的数据文件中的最大字段数

bash 编程 sed

sed编辑器                 流编辑器 可以基于输入到命令行的或是存储在命令文件中的命令来处理数据流中的数据,它每次从输入中读取一行,用提供的编辑命令匹配数据,按命令中指定的方式修改流中的数据,然后将生成的数据输出到STDOUT.默认所有来着STDIN的数据,都会输出的屏幕上,-n 选项 只要经常sed处理的哪一行才会输出到STDOUT上. 命令格式 sed option script file 选项 -e script  执行多个命令 -f file        执行文件中的

15、自学——Linux的学习进度与任务【bash编程之条件判断】

bash编程之条件判断 bash编程之条件判断:判定后续操作的前提条件是否满足 1.条件判断的常用判断类型(bash内生的): 整数测试 字符测试 文件测试   2.echo $?(执行状态返回值): 0:正确 1-255:错误 布尔值: 真 假 3.逻辑运算: 与运算: 真 && 真 = 真(0) 真 && 假 = 假(非0) 假 && 真 = 假 假 && 假 = 假 或运算: 真 || 真 = 真(0) 真 || 假 = 真 假 ||