我有以下要求。我有一个输入流,需要将其截断为某个固定长度的字节。我事先不知道输入流的长度。如果流的长度小于设置的长度,我想用零字节填充它。我尝试使用truncate,但显然它只能处理文件,不能处理标准输入。
例如,假设我们的输入流 (stdin) 是TEST,并且我们希望达到 10 个字节的长度。那么输出流(stdout)应该是TEST\x00\x00\x00\x00\x00.
为了澄清这一点,该示例是使用小字符串完成的,但结果对于大流(兆字节到千兆字节)应该表现良好。另外我目前使用的容器是基于Ubuntu的。
如果我使用“read -s”,它将删除现有行并将下一个提示带到同一行,如下所述。请让我知道此错误的任何解决方案。我需要使用“-s”来读取而不是在屏幕上回显密码。
脚本:
$ cat a.sh
printf "Enter the db name : "
read -r sourcedb
printf "Enter the source db username: "
read -r sourceuser
printf "Enter the source database password: "
read -s sourcepwd;
printf "Enter the target database username: "
read -r targetuser
Run Code Online (Sandbox Code Playgroud)
电流输出:
$ ./a.sh
Enter the db name : ora
Enter the db username: system
Enter the db password: Enter the target database username:
Run Code Online (Sandbox Code Playgroud)
期望的输出:
$ ./a.sh
Enter the db name : ora
Enter the …Run Code Online (Sandbox Code Playgroud) 该find命令有一个方便的-printf操作符,可以为每个找到的文件/文件夹打印用户指定的元数据。该命令有这样的选项吗ls?
作为替代方案,我可以将感兴趣的文件名列表提供给
find而不是ls,但这似乎就像在飞行中使用大锤一样。我已经有了感兴趣的文件,但我并没有真正“找到”任何东西。
此外,提供路径find会很棘手,因为我不能只将所需的文件路径附加到命令的末尾find
。该find命令要求路径位于运算符(或“谓词”)之前。因此,我无法轻松利用“xargs”。
感谢Steeldriver的回答。我认为stat如果我从头开始,他的使用将是一个解决方案。find不幸的是,我必须将输出与其他系统(特别是使用's )生成的类似信息进行比较printf。
以下是一些find我发现有效的代码习惯用法:
# Option 1
Some Command \
| xargs -n 1 -I{} find {} -printf '%p\t%TY-%Tm-%Td %TH:%TM\t%s\n'
# Option 2
Some Command | tr '\n' '\0' \
| xargs -0 -I{} find {} -printf '%p\t%TY-%Tm-%Td %TH:%TM\t%s\n'
Run Code Online (Sandbox Code Playgroud)
根据muru 的评论,这是一个我尚未使用的代码习惯用法,因为我的findCygwin 的 Gnu 版本早于 4.9 …
我有这样的事情:
A=$(curl https://mysite.com)
Run Code Online (Sandbox Code Playgroud)
并且curl请求返回字符串\"Hello World\"。当我现在想A使用以下之一打印到控制台时:
echo "$A"
printf '%s' "$A"
Run Code Online (Sandbox Code Playgroud)
消失\了,它只是说"Hello World"。我该怎么做才能进入\"Hello World\"控制台?
我正在尝试同时使用printf和export用于设置的变量,PS1但我尝试过的所有内容都返回错误printf或将文字\u放入提示中。
v='\u\$'; printf "$v"; export PS1="$v"
-bash: printf: missing unicode digit for \u
v='\\u\$'; printf "$v"; export PS1="$v"
\u$
v=$'\u'; v+='\$'; printf "${v}"; export PS1="${v}"
-bash: printf: missing unicode digit for \u
Run Code Online (Sandbox Code Playgroud)
有没有一种方法来格式化$v,以使双方printf和export工作?
如何在printf字符串中使用破折号?
x=xxx
printf -v args "-x=%s" "$x"
printf "$var"
Run Code Online (Sandbox Code Playgroud)
但它给出了一个错误:
./tmp.sh: line 2: printf: -x: invalid option
printf: usage: printf [-v var] format [arguments]
Run Code Online (Sandbox Code Playgroud)
我知道这是因为printf解释-x为一种选择,但如何克服它?
我printf用来创建一个字符串,该字符串将对应于命令行参数,如-x=xxx -y=yyy -z=zzz. 然后我想调用一个工具
eval $tool args
我正在尝试创建一系列带有大括号扩展的文件。fileA1我想创建具有所有可能组合的文件fileZ100(类似于touch file[A..Z][1..100])。
如果我运行命令,touch $(printf "file%d " {1..100})输出正常:
file1 file15 file21 file28 file34 file40 file47 file53 file6 file66 file72 file79 file85 file91 file98
file10 file16 file22 file29 file35 file41 file48 file54 file60 file67 file73 file8 file86 file92 file99
file100 file17 file23 file3 file36 file42 file49 file55 file61 file68 file74 file80 file87 file93
file11 file18 file24 file30 file37 file43 file5 file56 file62 file69 file75 file81 file88 file94
file12 file19 file25 file31 file38 file44 file50 …Run Code Online (Sandbox Code Playgroud) sh -c 'printf "%d " 024'
bash -c 'printf "%d " 024'
zsh -c 'printf "%d" 024'
Run Code Online (Sandbox Code Playgroud)
以上输出20 20 24。为什么 zsh 不尊重八进制表示法?有办法改变这个吗?
printf "%*s\n" $(((${#fname}+$COLUMNS)/2)) "$fname"
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
line 9: (7+)/2: syntax error: operand expected (error token is ")/2")
Run Code Online (Sandbox Code Playgroud)
这在终端中有效,但在我的脚本中无效。你有什么想法?
我想使用printf带有特定格式字符串的快捷命令,并提出了以下内容。
local PF="printf %s\n"
$PF "Some Text"
Run Code Online (Sandbox Code Playgroud)
它完成了这项工作,但想知道使用这种方法时是否存在任何警告,这可能会导致格式字符串的误解。