标签: shell

Python:在特定目录中运行shell命令

我使用python脚本编译maven项目.

如果我使用

from subprocess import call
call("mvn clean && mvn compile")
Run Code Online (Sandbox Code Playgroud)

我无法定义我的maven项目的目录,maven尝试在Python脚本的目录中编译.

是否有任何标准方法来定义shell命令的执行目录?

python shell

3
推荐指数
1
解决办法
4740
查看次数

使用awk格式化文本

我很难理解如何使用awk实现我想要的东西,经过相当长的一段时间后,我找不到我正在寻找的解决方案.

我有一个输入文本,如下所示:

Some text (possibly containing text within parenthesis).
Some other text
Another line (with something here) with some text
 (
Element 4
)
Another line
 (
Element 1, span 1 to 
Element 5, span 4
)
Another Line 
Run Code Online (Sandbox Code Playgroud)

我想正确格式化'('和')'之间的怪异线条.预期产量如下:

Some text (possibly containing text within parenthesis).
Some other text
Another line (with something here) with some text
(Element 4)
Another line
(Element 1, span 1 to Element 5, span 4)
Another Line 
Run Code Online (Sandbox Code Playgroud)

查看堆栈溢出我发现:
如何选择两个标记模式之间的行,这可能会出现awk/sed多次

所以我现在正在使用的是 echo $text | awk …

bash shell awk zsh sed

3
推荐指数
1
解决办法
411
查看次数

BASH语法检查调试模式故障?

我们可以bash -n script.sh用来验证shell脚本的语法.但是,当我尝试测试此函数时,我注意到此选项无法找到所有语法错误.

例如:

root@ubuntu:~/testenv# cat test 
#!/bin/bash
SEND=1
if [ "$SEND" -eq 0 ]
        echo no
fi
Run Code Online (Sandbox Code Playgroud)

现在,让我们测试脚本:

root@ubuntu:~/testenv# bash -n test 
test: line 5: syntax error near unexpected token `fi'
test: line 5: `fi'
Run Code Online (Sandbox Code Playgroud)

它工作正常.但是,如果我只删除其中一个括号:

root@ubuntu:~/testenv# cat test     
#!/bin/bash
SEND=1
if [ "$SEND" -eq 0 
then 
        echo no
fi
root@ubuntu:~/testenv# bash -n test 
root@ubuntu:~/testenv# 
Run Code Online (Sandbox Code Playgroud)

没啥事儿!

我还检查了bash的手册页,它描述了"-n"是:

 -n               Read  commands  but  do not execute them.  This may be used to check a
                  shell script for  syntax  errors. …
Run Code Online (Sandbox Code Playgroud)

linux bash shell

3
推荐指数
1
解决办法
65
查看次数

Zsh:测试是否存在与模式匹配的文件

我有一个glob模式PATTERN,并希望在Zsh中实现以下逻辑:

if a file matching *PATTERN* exists, then
     do this-and-that
end
Run Code Online (Sandbox Code Playgroud)

请注意,我不需要知道哪些文件实际匹配模式; 只有某个文件(或目录)的相关事实.

我用一个if语句找不到一个聪明的方法来做这个,所以我使用一个最多执行一次的"循环"来实现它:

for f in *PATTERN*(N)
do
  this-and-that
  break
done
Run Code Online (Sandbox Code Playgroud)

这样做:如果没有条目匹配PATTERN,则(N)导致错误消息被抑制并且循环被跳过; 否则,循环体执行一次.

问题:有没有办法让我可以通过一个if无循环实现我的目标?

shell zsh

3
推荐指数
2
解决办法
1220
查看次数

Bash变量在插入cURL命令时被屠杀

该脚本的第一部分如下所示:

curl -k -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Cache-Control: no-cache" -d 'username=username&password=password "https://get_token" >> token.txt
token=$(<'token.txt'$'\r')
echo "$token"
Run Code Online (Sandbox Code Playgroud)

echo $ token的输出看起来不错(417个字符):

eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyTVd0bGo4Y1VPYW8xNmdZTlV4QWlFS3RLcTc1b2JsSEN0c3hKWnlxeExCTkRKb1wvKzNSK1l5XC9YUFhoWExLaytJRVdKSE5CTE9MZmxPNksxemFjYXVlM0t1NnFIQjNRXC9QeUxzUTdVSmVOYk9pS3FDeHhcLzk1dz09IiwibmJmIjoxNDgzOTg5NjgwLCJpc3MiOiJwaHgtcmVtc3QtMDAzIiwiX2NhY2hlSWQiOjE0MzA0OCwiZXhwIjoxNDgzOTkzNDAwLCJpYXQiOjE0ODM5ODk4MDAsImp0aSI6IklER0FBNVYwR1pNU0pBTzk5QlVXTzhOMDY5VlRGTiJ9.H3vD-WfZBFXOII4k-cy7Ey2QM9YXytp31m9-Huj9vKw
Run Code Online (Sandbox Code Playgroud)

让我们看看当我尝试将令牌插入脚本的下一部分时会发生什么。

echo curl -k -X POST -H "Content-Type: application/json" -H "Authorization: AR-JWT "$token"" -H "Cache-Control: no-cache" -d '{ 
  "values":{
      
    "First_Name": "John",
    "Last_Name": "Doe",
  }
}  ' "https://randomurl.com"
Run Code Online (Sandbox Code Playgroud)

这是输出:

curl -k -X POST -H Content-Type: application/json -H Authorization: AR-JWT eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyTVd0bGo4Y1VPYW8xNmdZTlV4QWlFS3RLcTc1b2JsSEN0c3hKWnlxeExCTkRKb1wvKzNSK1l5XC9YUFhoWExLaytJRVdKSE5CTE9MZmxPNksxemFjYXVlM0t1NnFIQjNRXC9QeUxzUTdVSmVOYk9pS3FDeHhcLzk1dz0 -H Cache-Control: no-cache -d { OiJwaHgtcmVtc3QtMDAzIiwiX2NhY2hlSWQiOjE0MzA0OCwiZXhwIjoxNDgzOTkzNDAwLCJpYXQiOjE0ODM5ODk4MDAsImp0aSI6IklER0FBNVYwR1pNU0pBTzk5QlVXTzhOMDY5VlRGTiJ9.H3vD-WfZBFXOII4k-cy7Ey2QM9YXytp31m9-Huj9vKw
  "values":{
      
    "First_Name": "John",
    "Last_Name": "Doe",
  }
}   https://randomurl.com
Run Code Online (Sandbox Code Playgroud)

传递令牌时发生2件事

  • 令牌中的“ 9IiwibmJmIjoxNDgzOTg5NjgwLCJpc3Mi”被删除

  • 令牌的第二部分(在删除部分之后)插入主体 …

bash shell curl

3
推荐指数
1
解决办法
525
查看次数

从错误上自动退出bash shell脚本:set + e似乎没有完成这项工作

虽然我知道20年来shell脚本并不关心错误,但我默默地对这种粗心大意感到好笑.即使你明确要求他们不要吞下错误并遵循崩溃早期原则仍然不会发生.

参考自动退出bash shell脚本出错,set +e似乎没有完成这项工作,这里有一个简短的例子:

#!/bin/bash -vx
set +e
apt-get install nonexisting1
apt-get install nonexisting2
set -e
Run Code Online (Sandbox Code Playgroud)

输出:

#!/bin/bash -vx
set +e
+ set +e
apt-get install nonexisting1
+ apt-get install nonexisting1
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nonexisting1  <--- first error, now stop!
apt-get install nonexisting2           <--- why do we continue here?
+ apt-get install nonexisting2
Reading package lists... Done
Building dependency tree       
Reading state information... …
Run Code Online (Sandbox Code Playgroud)

linux error-handling bash shell ubuntu

3
推荐指数
2
解决办法
657
查看次数

为什么在bash中将stdin重定向到while循环中?

请考虑以下示例脚本:

#!/bin/sh

do_something() {
    echo $@
    return 1
}

cat <<EOF > sample.text
This is a sample text
It serves no other purpose
EOF

cat sample.text | while read arg1 arg2 arg3 arg4 arg5; do
    ret=0
    do_something "$arg1" "$sarg2" "$arg3" "$arg4" "$arg5" <&3 || ret=$?
done 3<&1
Run Code Online (Sandbox Code Playgroud)

重定向stdout为filedescriptor 3的输入的目的是什么?至少在Bash,如果省略它似乎没有任何区别.如果它在任何其他shell中执行,它会有什么影响bash吗?

UPDATE

对于那些想知道它来自哪里的人来说,它是来自Debian cryptdisks_start脚本的简化示例.

linux bash shell io-redirection

3
推荐指数
1
解决办法
1161
查看次数

如何使用sed用\(空格)替换空间?

当我使用sed用X替换所有空格时,命令有效,命令为:

sed 's/ /X/g' filelist.tmp
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用\ space替换所有出现的空间时,代码为:

sed 's/ /\ /g' filelist.tmp
Run Code Online (Sandbox Code Playgroud)

它不起作用.我究竟做错了什么?请注意,我是shell脚本的新手.

bash shell sed

3
推荐指数
1
解决办法
1万
查看次数

os.chdir()到相对主目录(/ home/usr /)

有没有办法使用os.chdir()去相关用户文件夹?

我正在做一个bash,我发现的唯一问题是cd ~,arg[0]因为我使用这个cd函数是未定义的:

def cd(args):
    os.chdir(args[0])
    return current_status
Run Code Online (Sandbox Code Playgroud)

我想改变的

def cd(args):
    if args[0] == '~':
        os.chdir('/home/') 
# Here I left it to /home/ since I don't know how 
# to get the user's folder name
    else:
        os.chdir(args[0])
    return current_status
Run Code Online (Sandbox Code Playgroud)

python bash shell chdir

3
推荐指数
1
解决办法
2820
查看次数

别名在脚本中丢失

我想在GNU最新别名gdate来date当程序在Mac上运行

#!/bin/bash
if [[ $(uname) -eq 'Darwin' ]]; then
    alias date="gdate"
    echo 'you are on a mac!'
    type date
fi
# rest of the program
Run Code Online (Sandbox Code Playgroud)

鉴于此代码,如果我直接在终端上运行int它打印:

you are on a mac!
date is an alias for gdate
Run Code Online (Sandbox Code Playgroud)

但是,如果我像./test.sh打印一样运行脚本本身:

you are on a mac!
date is /bin/date
Run Code Online (Sandbox Code Playgroud)

为什么不从脚本中应用别名?

macos bash shell

3
推荐指数
1
解决办法
83
查看次数

标签 统计

shell ×10

bash ×8

linux ×3

python ×2

sed ×2

zsh ×2

awk ×1

chdir ×1

curl ×1

error-handling ×1

io-redirection ×1

macos ×1

ubuntu ×1