我想生成一个随机字符串(例如密码、用户名等)。应该可以指定所需的长度(例如 13 个字符)。
我可以使用哪些工具?
(出于安全和隐私原因,最好是离线生成字符串,而不是在网站上在线生成。)
以下 bash 语法验证是否param不为空:
[[ ! -z $param ]]
Run Code Online (Sandbox Code Playgroud)
例如:
param=""
[[ ! -z $param ]] && echo "I am not zero"
Run Code Online (Sandbox Code Playgroud)
没有输出,很好。
但是当param除了一个(或多个)空格字符为空时,情况就不同了:
param=" " # one space
[[ ! -z $param ]] && echo "I am not zero"
Run Code Online (Sandbox Code Playgroud)
“我不是零”是输出。
如何更改测试以将仅包含空格字符的变量视为空?
我想删除字符串的最后一个字符,我尝试了这个小脚本:
#! /bin/sh
t="lkj"
t=${t:-2}
echo $t
Run Code Online (Sandbox Code Playgroud)
但它打印“lkj”,我做错了什么?
我搜索了 SO,发现将以下字符串大写是可行的
str="Some string"
echo ${str^^}
Run Code Online (Sandbox Code Playgroud)
但是我试图在命令行参数上做类似的事情,这给了我以下错误
#!/bin/bash
## Output
echo ${1^^} ## line 3: ${1^^}: bad substitution
echo {$1^^} ## No error, but output was still smaller case i.e. no effect
Run Code Online (Sandbox Code Playgroud)
我们怎么能做到这一点?
我有一个字符串:
one_two_three_four_five
Run Code Online (Sandbox Code Playgroud)
我需要从上面的字符串中保存一个变量A值two和变量B值four
我正在使用 ksh。
如何在输入中用新行替换空格,例如:
/path/to/file /path/to/file2 /path/to/file3 /path/to/file4 /path/to/file5 等等...
要获得以下内容:
/path/to/file
/path/to/file2
/path/to/file3
/path/to/file4
/path/to/file5
Run Code Online (Sandbox Code Playgroud)
我发布这个问题是为了帮助其他用户,在我开始输入这个问题之前,在 UNIX SE 上找到有用的答案并不容易。之后我发现了以下内容:
假设我有一个变量
line="This is where we select from a table."
Run Code Online (Sandbox Code Playgroud)
现在我想 grep 有多少次 select 在句子中出现。
grep -ci "select" $line
Run Code Online (Sandbox Code Playgroud)
我试过了,但没有用。我也试过
grep -ci "select" "$line"
Run Code Online (Sandbox Code Playgroud)
它仍然不起作用。我收到以下错误。
grep: This is where we select from a table.: No such file or directory
Run Code Online (Sandbox Code Playgroud) 当我echo * 得到以下输出时:
file1 file2 file3 ...
Run Code Online (Sandbox Code Playgroud)
我想要的是挑出第一个词。我该如何继续?
我有一个下一个格式的字符串
id;some text here with possible ; inside
Run Code Online (Sandbox Code Playgroud)
并希望通过第一次出现 将其拆分为 2 个字符串;。所以,它应该是:id和some text here with possible ; inside
我知道如何拆分字符串(例如,使用cut -d ';' -f1),但由于我;在左侧部分,因此它将拆分为更多部分。
在蟒蛇中
re.sub(r"(?<=.)(?=(?:...)+$)", ",", stroke )
Run Code Online (Sandbox Code Playgroud)
用三元组分割一个数字,例如:
echo 123456789 | python -c 'import sys;import re; print re.sub(r"(?<=.)(?=(?:...)+$)", ",", sys.stdin.read());'
123,456,789
Run Code Online (Sandbox Code Playgroud)
如何用 bash/awk 做同样的事情?