html tool

显示标签为“bash”的博文。显示所有博文
显示标签为“bash”的博文。显示所有博文

2024年2月28日星期三

转:shell分割字符

 参考:https://blog.csdn.net/u010003835/article/details/80750003

]# st="atbtst"
]# ar=(${st//t/ })
]# echo ar[0]
ar[0]
]# echo ${ar[0]}
a
]# echo ${ar[1]}
b

其中ar=(${st//t/ })参考:https://blog.csdn.net/u010003835/article/details/80749220
使用空格 替换$st变量中全部的t,定义为新的数组ar

2023年2月23日星期四

转:linux date获得指定时间

 参考:https://stackoverflow.com/questions/11144408/convert-string-to-date-in-bash

This worked for me :

date -d '20121212 7 days'
date -d '12-DEC-2012 7 days'
date -d '2012-12-12 7 days'
date -d '2012-12-12 4:10:10PM 7 days'
date -d '2012-12-12 16:10:55 7 days'

2022年10月13日星期四

time将指定时间转换为秒

 # date -d "2015-01-01 00:00:00" "+%s"

1420041600


openssl的sha1密钥 bash实现

 

来源:

https://blog.csdn.net/armlinuxww/article/details/115479401

https://www.qetool.com/scripts/view/22012.html

目标:

python如下代码的bash实现

# 通过hmac计算sha1,然后base64编码即可
signature = base64.encodestring(hmac.new(client_secret, string2Sign, hashlib.sha1).digest()).replace("\n", "")

bash实现
echo "${client_secret}"|openssl sha1 -binary -hmac ${string2Sign}|base64

其中 openssl sha1 -binary -hmac ${string2Sign}
sha1为信息完整性摘要的一种
# echo "add"|openssl dgst -sha1  -binary -hmac "aaa"|base64
6UbANJJ0F5LLE7tPy/zcKvRwxic=
# echo "add"|openssl sha1  -binary -hmac "aaa"|base64
6UbANJJ0F5LLE7tPy/zcKvRwxic=


[参考:
OpenSSL实现了5种信息摘要算法,分别是MD2、MD5、MDC2、SHA(SHA1)和RIPEMD

。SHA算法事实上包括了SHA和SHA1两种信息摘要算法,此外,OpenSSL还实现了DSS标准中规定的两种信息摘要算法DSS和DSS1。

摘要一般有两个作用:1)做信息完整性校验;2)保存密码,有些密码是直接在数据库中采用MD5(真实密码值)保存的,有的还进行加盐处理,使其难以破解,这样密码只能重置,无法告诉你原始过程,因为摘要是不可逆的。

 

  1. openssl dgst
  2. 常用选项有:
  3. [-md5|-md4|-md2|-sha1|-sha|-mdc2|-ripemd160|-dss1] :指定一种摘要算法
  4. -out filename:将摘要的内容保存到指定文件中
  5. dgst命令:
  6. 帮助: man dgst
  7. openssl dgst -md5 [-hex默认16进制] /PATH/SOMEFILE
  8. openssl dgst -md5 testfile
  9. md5sum /PATH/TO/SOMEFILE
  10. MAC: Message Authentication Code,单向加密的一种延伸应用,用于实现网络通
  11. 信中保证所传输数据的完整性机制
  12. CBC-MAC
  13. HMAC:使用md5或sha1算法

简单示例,对文件1进行求md5摘要:

]


转:bash中printf与echo的区别

 地址:https://blog.csdn.net/drbing/article/details/52905564

(1)首先echo是回显,即代表回车显示,是自带换行的;而printf只是打印出来,没有换行

(2)echo只是回显没有变量替换功能;printf是有的

举例:假如我们定义好变量a='hello world'

则 echo "%s" $a  显示的结果就是%s

而 printf "%s\n" $a  显示的结果就是hello world


bash中base64编解码

 [root@1 ngtdp_ruledemo_pcap]# printf "addd"|base64

YWRkZA==

[root@1 ngtdp_ruledemo_pcap]# printf "YWRkZA=="|base64 -d

addd

参见:https://www.jianshu.com/p/804c3fef6428

句法:

base64 [选项] [输入文件] [输出文件]

您可以在 base64 命令中使用不同类型的选项。在编码或解码时,可以从任何文件或标准输入中获取数据。编码或解码后,您可以将输出发送到文件中或在终端中打印输出。

选项:

-e 或 –encode

此选项用于对来自标准输入或任何文件的任何数据进行编码。它是默认选项。

-d 或 –decode

此选项用于解码来自标准输入或任何文件的任何编码数据。



2022年7月26日星期二

bash中使用固定长度随机数

参考:https://www.cnblogs.com/chengmo/archive/2010/10/23/1858879.html 通过内部系统变量($RANDOM) 其实,linux已经提供有个系统环境变量了,直接就是随机数,哈哈,觉得刚学习方法,是不是白费了!! [chengmo@centos5 shell]$ echo $RANDOM 10918 [chengmo@centos5 shell]$ echo $RANDOM 10001 #连续2次访问,结果不一样,这个数据是一个小于或等于5位的整数 可能有疑问了,如果超过5位的随机数怎么得到呢? 呵呵,加个固定10位整数,然后进行求余,跟例1 一样了。接下来的例子又是我们自立更生做了。

2019年2月26日星期二

bash反引号`使用




问题描述:

一个简单的bash for while
run_test()
{           
    EXEC_CMD=$1
    while [[ 1 == 1 ]]
    do   
        echo "${EXEC_CMD}"
        ${EXEC_CMD}                              #----------1
       
    done 
}           
run_test "bash replay_spe use_all"

1 的位置本来写的是
 `${EXEC_CMD}`

但是从执行结果上第二次无法执行 bash replay_spe use_all



解决:

修改为问题描述中没有反引号的代码就ok了,来解释一下

  • 反引号
两个反引号包围起来的字符串,将作为命令来运行,执行的输出结果作为该反引号的内容,称为命令替换
它有另一种更好的写法:$(command)
  反引号包围起来的字符串将被运行,取其结果

Shell script规范2.6.3节中写道:
Command substitution shall occur when the command is enclosed as follows:
$(command)
or (backquoted version):
`command`

而pope的目标是执行这个传入的 bash replay_spe use_all,而非执行这个 bash replay_spe use_all的屏幕输出,所以要在1中去掉反引号。
而错误中看到的第二次执行其实是while中的第一次执行,而第一次执行,是传入参数时就开始执行的。;)ok,6个icecream 值了,:)

参考:

https://www.jianshu.com/p/7f910c38e65e
https://www.jianshu.com/p/ed41f2e48464
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03

2017年10月22日星期日

bat 中判断字符串是否包含要求内容

http://www.bkjia.com/DOS_BAT/848821.html

@echo off
echo %1|findstr "^abc" >nul
if %errorlevel% equ 0 (
echo ok
) else (
echo not ok
)



set JSONPATH=D:\workbench\urlexample\target\urlexample-0.0.1-SNAPSHOT.jar;G:\selenium-java-3.5.3\org.json.jar
:: if "%CLASSPATH:JSONPATH=%"==%CLASSPATH%  (set CLASSPATH=D:\workbench\urlexample\target\urlexample-0.0.1-SNAPSHOT.jar;G:\selenium-java-3.5.3\org.json.jar;%CLASSPATH%) else echo 包含%JSONPATH%
echo %CLASSPATH%|find "%JSONPATH%"&& echo
if %errorlevel% equ 0 (
    echo has
) else (
    echo nohas
    set CLASSPATH=D:\workbench\urlexample\target\urlexample-0.0.1-SNAPSHOT.jar;G:\selenium-java-3.5.3\org.json.jar;%CLASSPATH%
)

echo %CLASSPATH%

2017年6月16日星期五

bash sudo 运行指定function

问题: bash function 中有部分需要sudo运行,如何定义
参考:https://serverfault.com/questions/177699/how-can-i-execute-a-bash-function-with-sudo?newreg=de7619fc54fe4547bd3e8af0647b860d


  1 #!/bin/bash                                                                                                                               
  2 #function run with sudo  
  3  
  4 function Hello() {
  5   echo "Heeeeeello, $1"
  6   whoami
  7   }
  8  
  9 hello(){
 10     echo "hi,$1"
 11 }
 12  
 13 hello nosudo
 14 sudo_password="sudo_password"
 15 echo "${sudo_password}"|sudo -S su root -c "$(declare -f Hello); Hello start"


$ help declare 

 Options:
      -f restrict action or display to function names and definitions