当前位置: 首页 > news >正文

运维Shell脚本小试牛刀(一)



运维Shell脚本小试牛刀(一)

运维Shell脚本小试牛刀(二)





一: Shell中循环剖析 for 循环.......

#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  countloop.sh
#                           USAGE: ./countloop.sh
#    DESCRIPTION:
#        OPTIONS: -------
#        REQUIREMENTS: ---------
#
#        BUGS: ------
#        NOTES: --------
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
for i in 1 2 3 4 5 6 #循环1~6
do

  echo "Thre for loop is run $i times."

done


echo "==============================================================================================================="

for linux in Debian Redhat Suse Fedora Centos7 Ubunto

do

echo "current out system is  ${linux}....."

done

echo "================================================================================================================"
 



#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  orcmdssub.sh
#                           USAGE: ./forcmdssub.sh
#    DESCRIPTION:
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
echo "Printing file list in /etc directory........"
for file in `ls /etc/*`
do 

   echo "current print file is: $file"

done
 



#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  forcmdssub.sh
#                           USAGE: ./forcmdssub.sh
#    DESCRIPTION:
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
echo "Printing file list in /etc directory........"
for file in `ls /etc/*`
do 

   echo "current print file is: $file"

done
[root@www shelldic]# cat forvarscoutents.sh 
#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  forvarscoutents.sh
#                           USAGE: ./forvarscoutents.sh
#    DESCRIPTION:
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================

filenames="/etc/my.cnf /etc/vconsole.conf  /etc/rsyncd.conf  /etc/sestatus.conf /usr/local/rs.conf"
# 上面的文件名以空格分割

for file in $filenames

do

  [ -f $file ] && echo "The file $file was found.." || echo "***** ERROR: The file $file was missing.....*******"

# 如果是文件,则打印文件存在,如果不存在则打印丢失

done
 



#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  simplenestedfor.sh
#                           USAGE: ./simplenestedfor.sh
#    DESCRIPTION:
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
for (( i = 0; i < 30; i++ )) #外循环

do
  
  for (( j=0; j<160; j++ ))  # 内循环
    
      do
         
          echo -n "* "  # 
       
      done

    echo "" # 
done 
 




二 : while循环 



#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  whichreadfile.sh
#                           USAGE: ./whichreadfile.sh
#    DESCRIPTION:
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================

file=$1  # 将位置参数1的值赋值给变量file

if [ $# -lt 1 ];then #  如果脚本未指定参数,则打印使用方法并退出运行

echo "Usage: $0 FILEPATH"
exit

fi

while read -r line  # 使用read命令从标准输入读取文件的一行,并赋值给变量line

do

echo $line  # 打印读取的行


done < "$file"


[root@www shelldic]# cat whilereadfile.sh 
#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  whichreadfile.sh
#                           USAGE: ./whichreadfile.sh
#    DESCRIPTION:
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================

file=$1  # 将位置参数1的值赋值给变量file

if [ $# -lt 1 ];then #  如果脚本未指定参数,则打印使用方法并退出运行

echo "Usage: $0 FILEPATH"
exit

fi

while read -r line  # 使用read命令从标准输入读取文件的一行,并赋值给变量line

do

echo $line  # 打印读取的行


done < "$file"
 

输出测试:

[root@www shelldic]# ./whilereadfile.sh /etc/sudo.conf 
#
# Default /etc/sudo.conf file
#
# Format:
# Plugin plugin_name plugin_path plugin_options ...
# Path askpass /path/to/askpass
# Path noexec /path/to/sudo_noexec.so
# Debug sudo /var/log/sudo_debug all@warn
# Set disable_coredump true
#
# Sudo plugins:
#
# The plugin_path is relative to ${prefix}/libexec unless fully qualified.
# The plugin_name corresponds to a global symbol in the plugin
# that contains the plugin interface structure.
# The plugin_options are optional.
#
# The sudoers plugin is used by default if no Plugin lines are present.
Plugin sudoers_policy sudoers.so
Plugin sudoers_io sudoers.so

#
# Sudo askpass:
#
# An askpass helper program may be specified to provide a graphical
# password prompt for "sudo -A" support. Sudo does not ship with its
# own passpass program but can use the OpenSSH askpass.
#
# Use the OpenSSH askpass
#Path askpass /usr/X11R6/bin/ssh-askpass
#
# Use the Gnome OpenSSH askpass
#Path askpass /usr/libexec/openssh/gnome-ssh-askpass

#
# Sudo noexec:
#
# Path to a shared library containing dummy versions of the execv(),
# execve() and fexecve() library functions that just return an error.
# This is used to implement the "noexec" functionality on systems that
# support C<LD_PRELOAD> or its equivalent.
# The compiled-in value is usually sufficient and should only be changed
# if you rename or move the sudo_noexec.so file.
#
#Path noexec /usr/libexec/sudo_noexec.so

#
# Core dumps:
#
# By default, sudo disables core dumps while it is executing (they
# are re-enabled for the command that is run).
# To aid in debugging sudo problems, you may wish to enable core
# dumps by setting "disable_coredump" to false.
#
# Set to false here so as not to interfere with /proc/sys/fs/suid_dumpable
#
Set disable_coredump false
 

http://www.lryc.cn/news/145204.html

相关文章:

  • screen命令,可以断开服务器连接,依旧能运行你的程序了
  • 【ArcGIS Pro二次开发】(63):批量更改字段别名
  • redis全套参数配置及降级解决方案
  • AMD即将上市大量中端显卡,为新显卡支付过高价格的日子可能结束
  • go学习一之go的初体验
  • 智能制造产业链数字化转型、数字化互联工厂建设方案PPT
  • 【安卓】拿注册码的两种方式
  • 【C++】—— 异常处理
  • cookie和session是什么,区别是什么?
  • linux的mmc子系统与块设备关联
  • 【Spring MVC】
  • 【ES】笔记-生成器函数与调用
  • 将Spring Boot与Redis集成
  • vue echarts
  • idea上利用JDBC连接MySQL数据库(8.1.0版)
  • 【100天精通python】Day47:python网络编程_Web编程基础
  • DockerCompose介绍与使用
  • Windows Qt 5.12.10下载与安装
  • RustDesk最新版本编译与打包
  • Gin 框架入门实战系列(一)
  • 【测试】pywinauto的简单使用(安装、常用对象、元素控件、鼠标操作、键盘操作)
  • Java基础十八(正则表达式 + 日期时间)
  • Linux C 多进程编程(面试考点)
  • c++一级
  • Code Lab - 34
  • 后端返回文件流,前端怎么导出、下载(8种方法可实现)
  • 什么是 ThreadLocal?
  • CANOCO5.0实现冗余分析(RDA)最详细步骤
  • 【tkinter 专栏】掷骰子游戏
  • 19 NAT穿透|python高级