我需要在c shell中将字符串值转换为整数。我将一个字符串变量从Python传递给c shell脚本,并在c shell中将其更改为整数。我传递的变量是br =“2”,但在c shell中需要将其更改为整数2。 任何允许我将整数值从python传递到c shell的解决方案也会有所帮助。
import subprocess
import shlex
var = "nest"
env_var = "16"
script = "./testshell.csh"
#prepare a command (append variable to the scriptname)
command = "{} {}".format(script, var)
#prepare environment variables
environment = {"test_var" : env_var}
#Note: shlex.split splits a textual command into a list suited for subprocess.call
subprocess.call( shlex.split(command), env = environment )
这是测试shell脚本
#!/bin/csh
set br = $test_var
@ nvar = br + 1
echo $nvar
错误是:@:表达式语法。
答案 0 :(得分:0)
问题是您忘记在br
前加上美元符号($
),它应该是:
@ nvar = $br + 1
示例:
% set br = '2'
% @ nvar = $br + 1
% echo $nvar
3