【问题标题】:Unable to set variable as a script for C-shell无法将变量设置为 C-shell 的脚本
【发布时间】:2020-12-05 11:09:58
【问题描述】:

这很简单。我正在尝试在第二个脚本中设置我的变量。我尝试了多种方法但不起作用。因此,所有注释掉的部分。您运行的第一个脚本 IE:unit3.2a.sh 3 5 这将返回 8。第二个脚本应将结果读取为 8。我正在使用 cygwin。

unit3.2a.sh '''

#!/bin/csh

set a = 0
set b = 0
set c = 0

@ a = $1
@ b = $2

@ c = $a + $b

echo $c

'''

unit3.sh '''

#!/bin/csh

a=$(~./unit3.2a.sh)
#below is all that don't work!
#instructors: a =~./unit3.2a.sh 2 200
#./unit3.2a.sh "2" "200"
#~./unit3.2a.sh 2 200
#=$(./unit3.2a.sh 2 200)
#=$(./unit3.2a.sh)+(2 200)
#$(./unit3.2a.sh 2 200)
#$(/home/Admin/unit3.2a.sh)
#$(/home/Admin/unit3.2a.sh 2 200)
#added echo $a to see if I wasnt seeing  anything the echo text works fine but a variable does not read?
#set a='./unit3.2a.sh 2 200'
##set a=$(cat ./unit3.2a.sh 2 200)
#$ ./unit3.sh
#cat: 2: No such file or directory
#cat: 200: No such file or directory
#set a=$(~./unit3.2a.sh 2 200)
#a=$(~./unit3.2a.sh 2 200)



echo $a

echo "The result is: $a"

echo "Storing the results.............."

echo "Storing the result: $a" >> unit3.txt

echo "The script is over"

'''

【问题讨论】:

  • 我不清楚您的用例在这里,但您知道cshsetenvset 及其区别吗? setenv 就像其他 shell 中的 export。它使变量对父csh 的子进程可见。祝你好运。

标签: linux bash cygwin csh tcsh


【解决方案1】:

请注意:

~./unit3.2a.sh 指的是特定的文件位置 而./unit3.2a.sh指的是当前目录下的文件

$( command ) 的形式是特定于 bash 的,在 C shell 中不起作用,请参阅
https://www.grymoire.com/Unix/Csh.html#uh-21

#!/bin/csh

set a = `./unit3.2a.sh 2 2`

echo $a
echo "The result is: $a"
echo "Storing the results.............."
echo "Storing the result: $a" >> unit3.txt
echo "The script is over"

产生:

$ ./unit3.sh
4
The result is: 4
Storing the results..............
The script is over

【讨论】:

  • 感谢文章,我懂python、java、c。但是linux正在踢我的两件套。它与 cmd 提示完全不同,就像我知道法语一样,所以我在阅读时听懂了很多西班牙语,但听不到任何线索。 :) 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
相关资源
最近更新 更多