I need to convert a string value to integer in c shell. I am passing a string variable from Python to c shell script and in c shell in need to change it to integer. The variable I am passing is say br ="2" but in c shell in need to change it to integer 2.
Any solution that allows me to pass an integer value from python to c shell will also help.
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 )
This is the test shell script
#!/bin/csh
set br = $test_var
# nvar = br + 1
echo $nvar
Error is: #: Expression Syntax.
The problem is that you forgot to prefix br with a dollar sign ($), it should be:
# nvar = $br + 1
Example:
% set br = '2'
% # nvar = $br + 1
% echo $nvar
3
Related
Here I am getting variables from Airflow GUI in form of JSON
example_vars=Variable.get("example_vars", deserialize_json=True)
dag = DAG('NEW_DAG', description='NEW_DAG', schedule_interval=None, default_args = default_args,catchup=False )
shell_command = 'path-to-my-file/my_script.sh '
running_task = BashOperator( task_id='task_to_run', bash_command=shell_command, trigger_rule="all_done",params={"val": example_vars}, dag=dag )
running_task
I tried from this page Airflow BashOperator: Passing parameter to external bash script
in the bash script---->
echo {{ params.val }}
and it prints {{ params.val }} not the json file.
You need
shell_command = 'path-to-my-file/my_script.sh "{{example_vars}}"'
See the examples at https://airflow.apache.org/docs/apache-airflow/stable/howto/operator/bash.html
import json
import shlex
# JSON variable
data = {'key': 'value'}
# Convert JSON variable to string
json_data = json.dumps(data)
# Quote the string to escape any special characters
escaped_json_data = shlex.quote(json_data)
# Pass the quoted string to the bash script
bash_command = './script.sh ' + escaped_json_data
# Create a BashOperator
bash_task = BashOperator(
task_id='bash_task',
bash_command=bash_command,
dag=dag)
In bash script:
#!/bin/bash
json_data=$1
I am trying to execute set of commands from jenkinsfile.
The problem is, when I try to assign the value of stdout to a variable it is not working.
I tried different combinations of double quotes and single quotes, but so far no luck.
Here I executed the script with latest version of jenkinsfile as well as old version. Putting shell commands inside """ """ is not allowing to create new variable and giving error like client_name command does not exist.
String nodeLabel = env.PrimaryNode ? env.PrimaryNode : "slave1"
echo "Running on node [${nodeLabel}]"
node("${nodeLabel}"){
sh "p4 print -q -o config.yml //c/test/gradle/hk/config.yml"
def config = readYaml file: 'devops-config.yml'
def out = sh (script:"client_name=${config.BasicVars.p4_client}; " +
'echo "client name: $client_name"' +
" cmd_output = p4 clients -e $client_name" +
' echo "out variable: $cmd_output"',returnStdout: true)
}
I want to assign the stdout from the command p4 clients -e $client_name to variable cmd_output.
But when I execute the code the error that is thrown is:
NoSuchPropertyException: client_name is not defined at line cmd_output = p4 clients -e $client_name
What am I missing here?
Your problem here is that all the $ are interpreted by jenkins when the string is in double quotes. So the first 2 times there's no problem since the first variable comes from jenkins and the second time it's a single quote string.
The the third variable is in a double quote string, therefore jenkins tries to replace the variable with its value but it can't find it since it's generated only when the shell script is executed.
The solution is to escape the $ in $client_name (or define client_name in an environment block).
I rewrote the block:
String nodeLabel = env.PrimaryNode ? env.PrimaryNode : "slave1"
echo "Running on node [${nodeLabel}]"
node("${nodeLabel}"){
sh "p4 print -q -o config.yml //c/test/gradle/hk/config.yml"
def config = readYaml file: 'devops-config.yml'
def out = sh (script: """
client_name=${config.BasicVars.p4_client}
echo "client name: \$client_name"
cmd_output = p4 clients -e \$client_name
echo "out variable: \$cmd_output"
""", returnStdout: true)
}
One thing that's really great in linux bash shell is that you can define variables inside of a subshell and after that subshell completes the (environment?) variables defined within are just gone provided you define them without exporting them and within the subshell.
for example:
$ (set bob=4)
$ echo $bob
$
No variable exists so no output.
I was also recently writing some powershell scripts and noticed that I kept having to null out my variables / objects at the end of the script; using a subshell equivalent in powershell would clear this up.
I've not heard of such functionality before, but you can get the same effect by running something like the following:
Clear-Host
$x = 3
& {
$a = 5
"inner a = $a"
"inner x = $x"
$x++
"inner x increment = $x"
}
"outer a = $a"
"outer x = $x"
Output:
inner a = 5
inner x = 3
inner x increment = 4
outer a =
outer x = 3
i.e. this uses the call operator (&) to run a script block ({...}).
I am writing a csh alias so that I can use the following bash function in my csh :
function up( )
{
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
P=$P/..
done
cd $P
export MPWD=$P
}
(I stole the above bash function from here)
I have written this:
alias up 'set LIMIT=$1; set P=$PWD; set counter = LIMIT; while[counter!=0] set counter = counter-1; P=$P/.. ; end cd $P; setenv MPWD=$P'
However, I am getting the following error:
while[counter!=0]: No match.
P=/net/devstorage/home/rghosh/..: Command not found.
end: Too many arguments.
and my script is not working as intended. I have been reading up on csh from here.
I am not an expert in csh and what I have written above is my first csh script. Please let me know what I am doing wrong.
You can also do this
alias up 'cd `yes ".." | head -n\!* | tr "\n" "\/"`'
yes ".." will repeat the string .. indefinitely; head will truncate it to the number passed as argument while calling the alias ( !* expands to the arguments passed; similar to $# ) and tr will convert the newlines to /.
radical7's answer seems to be more neat; but will only work for tcsh ( exactly wat you wanted ). This should work irrespective of the shell
You can use the csh's repeat function
alias up 'cd `pwd``repeat \!^ echo -n /..`'
No loops needed (which is handy, because while constructs in tcsh seem very finicky)
For multiple lines of code, aliases must be within single quotes, and each end of line must precede a backslash. The end of the last line must precede a single quote to delimit the end of the alias:
alias up 'set counter = $1\
set p = $cwd\
while $counter != 0\
# counter = $counter - 1\
set p = $p/..\
end\
cd $p\
setenv mpwd $p'
By the way, variables set with set are better with the equal sign separated from the variable name and content; setenv doesn't require an equal sign; math functionality is provided by #; control structures make use of parentheses (though aren't required for simple tests); use $cwd to print the current working directory.
I'm creating a script like this
#!/bin/csh
set h=1;
while [h=1] do echo "hi"; h=2; done;
but when I execute it a get this:
===> message after : csh test.sh [h=1]: No match.
Try:
set h = 1
while ( $h == 1 )
echo "hi"
set h = 2
end
You seem to be trying to mix Bourne shell syntax into your C shell script.
Csh is generally a lousy language for scripting, try to avoid it if at all possible
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
UPDATE:
The csh equivalent to read h is:
set h = $<