当myscript使用以下参数执行时,它将失败,错误为255(如下)
1 2 3 4 5 6 7
myscript value value /my/path/to/file my_file /tmp/ value value
Run Code Online (Sandbox Code Playgroud)
检查传递的参数数量
if [ ${#} -ne 7 ]
echo ${#} // Actually prints 7
then
echo "ERROR 255: Must provide the following 7 parameters:
one two three four five six seven"
exit 255
fi
Run Code Online (Sandbox Code Playgroud)
所以...如果数字不是7,退出,但要告诉数字是什么.. 7.
世界变得疯了吗?:)
我需要在每个具有值的 vmstat 行中插入日期时间。
我可以创建一个这样的函数:
function insert_datetime {
while read line
do
printf "$line"
date '+ %m-%d-%Y %H:%M:%S'
done
}
Run Code Online (Sandbox Code Playgroud)
然后调用 vmstat 如下:
'vmstat 3 5 | insert_datetime'
Run Code Online (Sandbox Code Playgroud)
但是这一行将日期时间放在每一行中,包括破折号 (--) 和任何包含文本的行。如何排除具有破折号和文本的行?
kthr memory page faults cpu 04-23-2013 10:19:49
----- ----------- ------------------------ ------------ ----------------------- 04-23-2013 10:19:49
r b avm fre re pi po fr sr cy in sy cs us sy id wa pc ec 04-23-2013 10:19:49
0 0 45688088 4094129 0 0 0 0 0 0 45 12172 2840 1 1 …Run Code Online (Sandbox Code Playgroud) 我有2个文件:
字符串列表:
ben
john
eric
Run Code Online (Sandbox Code Playgroud)
几行:
ben when to school
my mother went out
the dog is big
john has FB
eric is nice guy
Run Code Online (Sandbox Code Playgroud)
expoted文件:
my mother went out
the dog is big
Run Code Online (Sandbox Code Playgroud)
我想使用grep -v并删除列表中包含字符串的行.
这是想法,但错误的命令:
grep -v `cat file2` file1 > out
Run Code Online (Sandbox Code Playgroud)
谢谢
阿萨夫
我使用 sed 命令从文件中删除前两行和最后一行,我想将输出重定向到相同的文件
例子 :
sed '1,2d' '$d' pra >pra
Run Code Online (Sandbox Code Playgroud)
这会从文件中删除前两行和最后一行。但是当我尝试将输出重定向到同一文件时,就会出现问题。
当我打开并看到重定向的文件时,文件中pra没有任何内容。
有没有更好的方法来做到这一点..从文件中删除前两行和最后一行,并用相同的文件名保存它。
STRINGS=str1,str2,str3
Run Code Online (Sandbox Code Playgroud)
我希望每个字符串按空格分隔,如下所示:
STRING1=`echo $STRINGS | cut -d',' -f1` ==> gives "str1"
REMAINING_STRING=`echo $STRINGS | cut -d',' -f2- |
sed -e 's/,/ /g'` ==> gives "str2 str3"
Run Code Online (Sandbox Code Playgroud)
但是当字符串只包含一个条目时,例如STRINGS = str1,则REMAINING_STRING也会填充与STRING1相同的值.当STRINGS只包含一个条目时,我希望REMAINING_STRING为null.
STRINGS=str1
STRING1=`echo $STRINGS| cut -d',' -f1` ==> gives "str1"
REMAINING_STRING=`echo $STRINGS | cut -d',' -f2- | sed -e 's/,/ /g'`
Run Code Online (Sandbox Code Playgroud)
==>给出"str1",但这应该是null.
如何在unix shell中执行此操作?
我正在尝试在 unix 中使用 sendmail 功能,但似乎无法通过附件...我的代码如下:
export MAILTO="me@myco.com"
export SUBJECT="test mail"
export ATTACH="/home/tstattach"
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo "this is a test message"
base64 $ATTACH
) | sendmail $MAILTO
Run Code Online (Sandbox Code Playgroud)
我的电子邮件消息是这样发送的,没有附件:
this is a test message
dGVzdCBhdHRhY2htZW50
---q1w2e3r4t5--
Run Code Online (Sandbox Code Playgroud)
我如何让附件通过?它似乎以一种奇怪的方式对其进行编码......我也尝试过 uuencode 而不是 base64 但后来我收到一个错误,说找不到 uuencode ......
我正在尝试编写一个我logAction专门调用的函数,该函数Ksh从管道读取输入并在同一个函数调用中接受参数。我一直在尝试调试这个 2 周,在谷歌和这样的网站上搜索。
我尝试创建的函数的目的是简化将输出记录到我开发的其他脚本的日志文件的过程。我打算在各种情况下使用该命令,但最容易描述和说明我正在尝试的两种情况类似于以下内容:
语法示例 1:
logAction 1 "Script executed some action."
Run Code Online (Sandbox Code Playgroud)
语法示例 2:
COMMAND1 | COMMAND2 | logAction 1
Run Code Online (Sandbox Code Playgroud)
如您所见,我希望能够将预期的日志信息作为参数或作为 stdinput 通过管道传递给函数,以便捕获命令序列的输出。第三种情况是在管道输出之前添加一些文本,即
语法示例 3:
COMMAND1 | COMMAND2 | logAction 1 "This is the output: "
Run Code Online (Sandbox Code Playgroud)
下面的输出说明了使用示例 3 中的语法的结果:
[2015/12/28 10:20:32] This is the output: <Output from COMMAND1 | COMMAND 2>
Run Code Online (Sandbox Code Playgroud)
这是我尝试创建的函数的基本框架。
function logAction {
while read -t 0 -u 0 stdinput
do
echo "StdInput:'$stdinput'"
pipedinput=$stdinput
done
pipedinput=${pipedinput:-}
case $1 in
1) #Output text only to …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么我的IFS语句正在替换输出文件中的字符吗?为了解决这个问题,我需要改变什么?我想逐行读取一个输入文件(逐个记录)和完全忽略的IFS,但我不知道该放什么,以便我把它放进去.
#!/bin/bash
SAVEIFS=$IFS
IFS='\n'
record="The rain in Spain"
record2="c:\user\user\document.txt"
echo $record
echo $record
Run Code Online (Sandbox Code Playgroud)
我的输出是用空格替换所有我的小写'n'字符,我的'\'字符被删除,而不是(我认为会发生)用新行分隔:
The rai i Spai
c: user user docume t.txt
Run Code Online (Sandbox Code Playgroud)
提前致谢
我有以下json输入
... "somefield":"somevalue", "time":"timevalue", "anotherfield":"value" ...
Run Code Online (Sandbox Code Playgroud)
在我的ksh脚本中,我希望用我的值替换timevalue.所以我使用组合创建了这个正则表达式
data=`cat somefile.json`
echo $data | perl -pe "s|(.*time\"\s*\:\s*\").*?(\".*)|\1%TIME%\2|g" | another-script.sh
... "somefield":"somevalue", "time":"%TIME%", "anotherfield":"value" ...
Run Code Online (Sandbox Code Playgroud)
但是...我不能使用数字作为替换,因为perl使用数字来定义组..所以这个显然不起作用
perl -pe "s|(.*time\"\s*\:\s*\").*?(\".*)|\120:00:00\2|g"
Run Code Online (Sandbox Code Playgroud)
我可以通过两步替换来克服这个问题
perl -pe "s|(.*time\"\s*\:\s*\").*?(\".*)|\1%TIME%\2|g" | perl -pe "s|%TIME%|20:00:00|"
... "somefield":"somevalue", "time":"20:00:00", "anotherfield":"value" ...
Run Code Online (Sandbox Code Playgroud)
但我相信有一种更好,更优雅的方式