简体   繁体   English

在shell脚本中编写$

[英]Writing $ inside the shell script

I have a problem while I use $ in my script 我在脚本中使用$时遇到问题

#!/bin/sh

grep -e ' H2O ' molfra.out > H2O
grep -e ' C6F13 ' molfra_ig.out > C6F13
grep -e ' C8F17 ' molfra_ig.out > C8F17
grep -e ' C10F21 ' molfra_ig.out > C10F21
grep -e ' C16F33 ' molfra_ig.out > C16F33

cat > SNW_CFscheck.gnplt << EOF
reset
set terminal png enhanced
set output '800K_Chains.png'
set title "250 water and PFPE chained SNW 800K NVT MD"
set yrange [0:4]
set ytics 1
set grid
set xlabel "Time(ps)"
set ylabel "Number of molecules"
plot "C6F13" u ($1/10000):2 w l title "800K C_6F_{13}", \
  "C8F17" u ($1/10000):2 w l title "800K C_8F_{17}", \
  "C10F21" u ($1/10000):2 w l title "800K C_{10}F_{21}", \
  "C16F33" u ($1/10000):2 w l title "800K C_{16}F_{33}"
EOF

gnuplot SNW_CFscheck.gnplt

So, I create a file named 'SNW_CFscheck.gnplt' using cat command, and execute the file But $1 is not recognized inside the cat command, (or $1 is recognized as global variable?) so the code doesn't work. 因此,我使用cat命令创建了一个名为“ SNW_CFscheck.gnplt”的文件,并执行该文件,但是cat命令中未识别$ 1,(或$ 1被识别为全局变量?),因此代码无法正常工作。

How can I use $ inside this scripts? 如何在此脚本中使用$?

Thanks 谢谢

If you want $1 not to be expanded then quote the EOF like this: 如果您不希望扩展$1 ,请像这样引用EOF:

cat > SNW_CFscheck.gnplt << 'EOF'
reset
set terminal png enhanced
set output '800K_Chains.png'
set title "250 water and PFPE chained SNW 800K NVT MD"
set yrange [0:4]
set ytics 1
set grid
set xlabel "Time(ps)"
set ylabel "Number of molecules"
plot "C6F13" u ($1/10000):2 w l title "800K C_6F_{13}", \
  "C8F17" u ($1/10000):2 w l title "800K C_8F_{17}", \
  "C10F21" u ($1/10000):2 w l title "800K C_{10}F_{21}", \
  "C16F33" u ($1/10000):2 w l title "800K C_{16}F_{33}"
EOF

As per man bash : 按照每个man bash

If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. 如果在word中引用了任何字符,则分隔符是对word删除引用的结果,并且本文档中的行不会扩展。

Did you try using an escape character? 您是否尝试过使用转义字符? Like "\\$"? 像“ \\ $”一样?

Try escaping the character within double quotes "\\$". 尝试转义双引号“ \\ $”中的字符。 Read more about the effect of Single vs Double quotes here ---> http://linux.101hacks.com/bash-scripting/quotes-inside-shell-script/ 在此处阅读有关单引号和双引号的影响的更多信息---> http://linux.101hacks.com/bash-scripting/quotes-inside-shell-script/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM