[关闭]
@mrz1 2017-12-26T13:41:06.000000Z 字数 2771 阅读 1085

linux-面试题

面试题


1. 随机换颜色

  1. [root@centos7 ~]#color=`seq 31 37|sort -R|head -1`;echo -e "\033[${color}mred color\033[0m"
  2. red color

1.1 用vim编写带颜色的字:

  1. 1. ctrl+v+[
  2. 2. [
  3. 3. 颜色
  4. 4. 内容
  5. 5.ctrl+v+[
  6. 6. [
  7. 7. 0m
  8. 例子:[[31mred^[[0m
  9. 必须用vim编写,如果不是,只会当做字符串显示,不会带颜色
  10. ^[是控制符一个整体
  11. 还有一种方法是echo 输进去

2. 计算1+2+3+...+100

  1. [root@centos7 ~]#echo {1..100} |tr ' ' '+'|bc -l //5050
  2. [root@centos7 ~]#seq -s + 1 100|bc
  1. [root@centos7 ~]#expr 1 + 799 //expr也可以进行运算
  2. 800
  1. [root@centos6 ~]#for (( sum=0,i=1;i<=100;i++ ));do let sum+=i;done ;echo $sum
  2. 5050

计算 3 6 9 99 相加值

  1. [root@centos6 ~]#for (( sum=0,i=0;i<=100;i+=3 ));do let sum+=i;done ;echo $sum
  2. 1683
  1. [root@centos6 ~]#sum=0;for i in {0..100..3};do let sum+=i;done;echo $sum
  2. 1683
  1. [root@centos6 ~]#sum=0;i=0;while [ $i -le 100 ]; do let sum+=i;let i+=3;done;echo sum=$sum
  2. sum=1683

3. 判断数字

var=haha123 ; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false

4. 判断正整数

var=0123 ; [[ "$var" =~ ^0*[1-9][0-9]*$ ]] && echo true || echo false

5. 手机号

mobile=12800138000 ;[[ "$mobile" =~ ^1[3456789][0-9]{9}$ ]] && echo true || echo false

6. 判断ip是否合法

  1. [root@centos7 ~]#ip="123.23.223.23";[[ "$ip" =~ (([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) ]] && echo true ||echo false
  2. true

判断数字

var=haha123 ; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false

判断正整数

var=0123 ; [[ "$var" =~ ^0*[1-9][0-9]*$ ]] && echo true || echo false

手机号

mobile=12800138000 ;[[ "$mobile" =~ ^1[3456789][0-9]{9}$ ]] && echo true || echo false

鸡兔同笼问题?

  1. [root@centos7 ~]#cat checkdisk.sh
  2. #! /bin/bash
  3. # ------------------------------------------
  4. # Filename: checkdisk.sh
  5. ......省略
  6. # ------------------------------------------
  7. echo "鸡兔同笼问题?"
  8. read -p "请输入头:" head
  9. read -p "请输入脚:" foot
  10. c=$[( foot - head*2 ) /2 ]
  11. r=$[ head - $c ]
  12. echo "兔子:$c"
  13. echo "鸡: $r"

查找f1和f2交集

  1. [root@centos6 app]#cat f1
  2. a
  3. e
  4. f
  5. c
  6. d
  7. [root@centos6 app]#cat f2
  8. a
  9. c
  10. b
  11. c
  12. g
  13. g
  14. d
  15. [root@centos6 app]#grep -f f2 f1 //把f2当成搜索条件去f1里面找交集
  16. a
  17. c
  18. d

RAID-0、RAID-1、RAID-10、RAID-01、RAID-5

随机口令

  1. [root@centos7 ~]#openssl rand -hex 4 //第一种
  2. a856ece3
  3. [root@centos7 ~]#openssl rand -base64 10|head -c 8 //第二种
  4. kuO114wH
  5. [root@centos7 ~]#cat /dev/random |tr -dc 'a-zA-Z0-9'|head -c8 //第三种
  6. Iv2uUkHQ

九九乘法表

  1. for ((i=1;i<=9;i++));do for ((j=1;j<=i;j++));do echo -ne "${j}x${i}=$[$i*$j]\t";done;echo;done

同步时间

ntpdate ip地址
例ntpdate 127.0.0.0 同步一次127.0.0.0服务器时间
永久同步时间需要修改/etc/ntp.conf

永久同步时间

  1. 永久同步时间需要修改/etc/ntp.conf
  2. [root@centos6 ~]#chkconfig --list ntpd
  3. ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  4. [root@centos6 ~]#chkconfig ntpd on
  5. [root@centos6 ~]#chkconfig --list ntpd
  6. ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  7. [root@centos6 ~]#service ntpd start
  8. Starting ntpd: [ OK ]

7

  1. 永久同步时间需要修改/etc/ntp.conf
  2. [root@centos7 ~]systemctl status ntpd
  3. ntpd.service - Network Time Service
  4. Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
  5. Active: inactive(无效的) (dead)
  6. [root@centos7 ~]#systemctl is-enabled ntpd
  7. disabled(禁用)
  8. [root@centos7 ~]#systemctl enable ntpd
  9. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
  10. [root@centos7 ~]#systemctl start ntpd
  11. [root@centos7 ~]#systemctl status ntpd
  12. ntpd.service - Network Time Service
  13. .....
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注