我正在编写一个shell脚本,需要从文本文件中提取值,如下所示:
app.full.name /warfilelocation/ warfilename
Run Code Online (Sandbox Code Playgroud)
我的shell脚本将遍历应用程序名称列表,并使用AWK提取位置或名称.我已使用以下命令在命令行上测试过:awk"\ $ 1~/app.full.name/{print $ 2}"applications.txt
返回我期望的东西然而当我把它放在一个shell脚本中我开始有问题.
我有一个看起来像这样的函数:
function get_location() {
local application=$1
awk "\$1 ~/^$application/ { print \$2 }" applications.txt
}
Run Code Online (Sandbox Code Playgroud)
但是,当我调用此函数时,我收到以下错误:
awk: $1 ~/^app.full.name
awk: ^ unterminated regexp
awk: cmd. line:1: app.full.name
awk: cmd. line:1: ^ syntax error
awk: cmd. line:2: app.full.name/ { print $2 }
awk: cmd. line:2: ^ syntax error
Run Code Online (Sandbox Code Playgroud)
有没有人有任何想法我在这里做错了什么.我认为我没有逃避变量的正确,但无论我尝试它似乎都不起作用.
提前致谢
每当我执行输出多行的linux命令时,我想对输出的每一行执行一些操作.我一般都这么做
command something | while read a
do
some operation on $a;
done
Run Code Online (Sandbox Code Playgroud)
这很好用.但我的问题是,是否有一些我可以通过预定义的符号访问每一行(不知道如何调用它)///像$这样的东西?..或.. $!..或.. $ _
有可能吗?
cat to_be_removed.txt | rm -f $LINE
Run Code Online (Sandbox Code Playgroud)
在bash中有预定义的$ LINE ..或者前一个是最短的方式.即.
cat to_be_removed.txt | while read line; do rm -f $line; done;
Run Code Online (Sandbox Code Playgroud) 在脚本中,我想逐行读取流程输出,并获得用户的确认.到目前为止我已经这样做了:
mycommand-outputpiped | while (read line)
do
read line
#dostuff
read confirm #oops -> this read the next item from the pipe, not the keyboard
done
Run Code Online (Sandbox Code Playgroud)
所以我试着添加:
read confirm < /dev/stdin
Run Code Online (Sandbox Code Playgroud)
但它并没有改变它的东西,它仍然从管道中读取下一行......我该如何处理?
我正在尝试远程运行shell脚本.我在一台机器上有一个资源(.txt文件),在另一台机器上有一个shell脚本,但是在同一个网络中.该脚本驻留在特定目录中,并且该脚本获取一些二进制文件,这些文件驻留在目标计算机中的所有其他目录中.现在,我无法远程运行脚本.但我可以运行一个没有任何本地依赖项的简单shell程序.你能建议一个解决方案来解决这个问题吗?
这是我用来运行shell-script-without-any-dependencies的命令.
cat input.txt | ssh clrg@192.168.2.22 "sh path/to/shell/script/tokenize.sh"
Run Code Online (Sandbox Code Playgroud)
在运行具有依赖项的脚本时,我收到以下错误,
"ambiguous redirect"
../../deter-4/bin/determine: No such file or directory
../../deter-4/bin/determine1: No such file or directory
Run Code Online (Sandbox Code Playgroud)
现在,如何在运行shell脚本时告诉接受依赖项
谁能看到我在这里做错了什么?我不断收到以下错误消息:[[:找不到
read INPUT
if [[ "$INPUT" -ge 1 ]] && [[ "$INPUT" -le 10 ]]; then
Do something
else
printf "Please enter a value between 1 and 10"
fi
Run Code Online (Sandbox Code Playgroud) 所以我正在编写一个bash shell脚本,我的前几行看起来像这样:
if ! [ $# -eq 0 || $# -eq 1 ]; then
echo -e "Usage: myScriptName [\e[3mdir\e[0m] [\e[3m-f file\e[0m]"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,它会说"[:missing`]'".我没有看到失踪,除了; 正在触动],所以我错过了什么?
Conf.txt
Enable_FUN Yes
FUN_Privilege_Level User
Force_FUN_Payload_Authentication No
Force_FUN_Payload_Encryption No
Character_Accumulate_Interval 12
Character_Send_Threshold 96
FUN_Retry_Count 7
FUN_Retry_Interval 50
Non_Volatile_Bit_Rate 115200
Volatile_Bit_Rate 115200
Run Code Online (Sandbox Code Playgroud)
我需要将"Volatile_Bit_Rate"值从115200更新到38400注意:Volatile_Bit_Rate和Non_Volatile_Bit_Rate具有相同的匹配字符串,格式(空格/制表符)也应保持不变.
我正在尝试以下方式,这是有效的.
var=38400
sed -i "s/\bVolatile_Bit_Rate\b/Volatile_Bit_Rate $var/" solconf
Run Code Online (Sandbox Code Playgroud)
然后我需要删除字符串的结尾.
但是,任何人都可以通过以下方式向我推荐正则表达式,这将有所帮助.
sed -i "s/\bVolatile_Bit_Rate\b(expression for allspaces)(somenumbers) /Volatile_Bit_Rate(spaces)$var/" solconf
Run Code Online (Sandbox Code Playgroud) 我在循环中有一个if语句.它最初设置为false,所以我在循环的第一次运行时在文件中插入一个时间戳.
我似乎无法得到以下正确评估.
$ConnectionIsCurrently=false
if ! [ $ConnectionIsCurrently ]; then
# changing false to true so this only occurs once.
$ConnectionIsCurrently=true
fi
Run Code Online (Sandbox Code Playgroud)
这是完整的循环:
while [ $i -le $NoOfTests ]; do
ping -c1 -t1 www.google.ie > /dev/null
if [ $? = 0 ]; then
ConTestPASSCount=$((ConTestPASSCount+1))
if ! [ $ConnectionIsCurrently ]; then
printf 'PASSED AT: '
date "+%s"
printf 'PASSED AT: ' >> $directory$LogFile
date "+%s" >> $directory$LogFile
ConnectionIsCurrently=true
fi
echo "PASSCount $ConTestPASSCount"
else
ConTestFAILCount=$((ConTestFAILCount+1))
if [ $ConnectionIsCurrently ]; then
printf 'FAILED AT: …Run Code Online (Sandbox Code Playgroud) 我已经在BASH if-then语句中查看了句柄函数返回值的各种方法,但似乎都没有.这就是我所拥有的
function is_cloned()
{
if [ -d $DIR_NAME ]
then
return $SUCCESS
fi
return $FAILURE
}
Run Code Online (Sandbox Code Playgroud)
如果我自己调用它并检查返回值,它将起作用:
is_cloned
retval=$?
if [ $retval -eq $FAILURE ]
then
...
fi
Run Code Online (Sandbox Code Playgroud)
如何在if语句中使用函数调用?或者根本没有办法利用返回值?
我有以下要简化的If-Else语句。
if [[ "$IP" == 192.* ]] || [[ "$IPAddr" == 193.* ]]
then
data="correct data set"
fi
Run Code Online (Sandbox Code Playgroud)
我需要包括更多内容,[[ "$IP" == 192.* ]]并希望查看是否有更好的方法可以做到这一点,而不是使用太多的||语句