Linux Shell笔记整理

shell相关

首行bash声明

#!/bin/bash

定义数组和引用数组

demo_lst=("demo_str1","demo_str2")
for i in ${demo_lst[*]}

符号相关

$()与反引号作用一致,${}则是引用变量,通常可忽略花括号,
但更为规范,可为解释器界定变量边界。

定义函数的格式

function(){}

.for循环

以do done包裹处理语句;无冒号;处理语句需缩进;

if语句

if [] then else fi的固定语法格式;无冒号;条件写法;

判断目录是否存在,文件是否存在。 -d -f

#时间处理的"+"号
#cur_time and its style of timestamp
date +"%Y-%m-%d %H:%M:%S" 
date +%s
#date -d time transformer 
date -d "2020-10-10 00:00:00" +%s
date -d @timestamp "%Y-%m-%d %H:%M:%S"
#find 
#逻辑连接格式;写法;
-o
-a
$(find "/mnt/"${one}"/" -mmin +5 -mmin -36 -type f \( -iname "J*.csv" -o -iname "J*.txt" -o -iname "J*.xml" \))
echo $(date +"%Y-%m-%d %H:%M:%S") >JLMCDA10_config.txt
last_cpytime=$(cat /opt/toolbox/scripts/JLMCDA10_config.txt)

关于管道与命令连用

find . -iname '*.cst' -exec cp {} ${des} \;
/bin/find $src -type f -iname "J*.csv" -print0 | xargs -0 /bin/tar -czvf final_res_csv.tar.gz \;

关于分隔符

OLD_IFS=$IFS

IFS='\n'

IFS=${OLD_IFS}

常见的LINunx命令篇;

tar -czvf ./* new_name.tar.gz
tar -xzvf

获取yum的仓库配置文件。

cat /etc/yum.repos.d/facicem.repo

下载C++编译器,python开发包

yum install gcc-c++ python-devel

上下文显示行数参数,-C

zcat ./* |grep 'seach_string' -C 20

用户相关

passwd命令

#使用root账号可以使用:
passwd llljjjqqq更改普通用户密码
#普通用户只能更改自己密码
passwd

useradd -d 指定登入目录

###更改组,用户主目录
usermod

monitor='JLMCDA10'
des_dir='cd'



incre_cpy(){
    prod_arr=$(cd /mnt/JLMCDA10/ ; ls -d */ )
    for prod in prod_arr
    do
        if [ -d /mnt/${des}/${monitor}/${prod} ];then
            echo "exist"
        else
            mkdir /mnt/${des}/${monitor}/${prod}
        for 
    if [condition ==0 ];then
        for element in $(find /mnt/${monitor}/${prod} -type f -inama "*.cst" -mmin +5 -mmin -36)
            file_name=$(basename ${element})
            if [ -f /mnt/${des}/${monitor}/${prod}${file_name} ];then
                echo 'exist file'
            else
                cp ${element} /mnt/${des}/${monitor}/${prod}
            fi
    else 
        end_time_stm=$(($(date -d ${2} +%s)-300))
        end_time=$(date -d @${end_time_stm} +"%Y-%m-%d %H:%M:%S")
        for element in $(find /mnt/${monitor}/${prod} -iname "*.cst" -newermt ${1} ! -newermt ${end_time}  )
            file_name=$(basename ${element})
            if [ -f /mnt/${des}/${monitor}/${prod}${file_name} ];then
                echo 'exist file'
            else
                cp ${element} /mnt/${des}/${monitor}/${prod}
            fi
    fi              
    done
    
}

main(){
st=$(date -d ${1} +%s)
et=${date -d ${2} +%s}
diff_t=$(( (et - st)/60))
if [ diff -le 30];then
    condition_code=0
else
    condition_code=1
fi
incre_cpy $last_cpytime $cur_time $condition_code
}



last_cpytime=$(cat /opt/toolbox/scripts/JLMCDA10_config.txt)
cur_time=$(date +"%Y-%m-%d %H:%M:%S")

main last_cpytime cur_time condition 

function split(){
OLD_IFS=$IFS
IFS="/"
array=($1)
IFS=OLD_IFS
echo ${array[$2]}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容