设置命令行参数

设置命令行参数

如何将“变量”设置为某些 CLI 程序的命令行参数。

我以前在 centos 上是这样做的:

    set var = "-x"
someprogram $var --other-parameters

我如何在 ubuntu 上执行此操作?

这是我正在使用的 shell 脚本:

#!/bin/csh

#####
#    Define Mercalli projection: Center_lon/Center_Lat/Plot_Width   
#####
set PROJ = "-Jm1.4"

#####
#    Define map bounds: MINLON/MAXLON/MINLAT/MAXLAT 
#####
#set REG = "-R38.0/47.0/34.0/37.0"
set REG = "-R40.0/49.0/28.0/38.0"
/bin/rm gmt.ps

######
#   cut region from "middlEST.grd" and name "map.grd"
######
#grdcut middlEST.grd -Gmap.grd $REG
grdcut topo1.grd -Gmap.grd $REG
grdcut topo1.grd -R-38/-49/28/38 -Gnecar.grd
grdcut topo1.norm -R-38/-49/28/38 -Gnecar.grd


######
#   Compute the gradient of topography
######
grdgradient map.grd -Gmap1.grd -A0 -Nt1.0 -V -M
#grdgradient map.grd -Gmap1.grd -A0 -Ne0.5 -V -M

######
#   USING grdhsuteq
######
grdhisteq map1.grd -Gmap2.grd -N

######
#   USING grdmath
######
grdmath map2.grd 0.1 MUL = map2.grd

######
#   plot topography in 2-D color using "relief1.cpt, relief2.cpt, topo.cpt, or globe.cpt"; 
#   write to postscript file; remove map.grd
######
grdimage map.grd -Ctopo.cpt $REG $PROJ -Imap2.grd -V -K > gmt.ps
#grdimage map.grd -Crelief1.cpt $REG $PROJ -Imap2.grd -V -K > gmt.ps 
#grdimage map.grd -Crelief2.cpt $REG $PROJ -Imap2.grd -V -K > gmt.ps
#grdimage map.grd -Cglobe.cpt $REG $PROJ -Imap2.grd -V -K > gmt.ps
/bin/rm map.grd

######
# to plot coastlines, pscoast options:
#   -Rw/e/s/n : the map boundarys
#   -K : allow more operations to add into current map "map.ps"
#   -O : overlay current operation to map "map.ps"
# projections:  
#   -Jx ==> black-white frame
#   -Jb ==> no black_white frame only major tick marks
#   -Jm : mecator projection
#   (1.40: the number following the projection option means 1.4 inch
#        for 1 degree)
#
#   -S : symbol
#       -Sc circle
#       -Sa star
#       -Sd diamond
#       -Si inverted triangle
#       -St triangle
#       -Ss square
#
#   -G200 : fill the inner area of symbol with 200 in gray scale 
#       (0-255) (black-white)
#   -Gr/g/b : fill with color; 3 elementary color REG GREEN BLUE in scale
#       (0-255) (dark-bright) 
#
#   -P : portrait
#
#   -D : resolution (f=full, h=high, i=intermediate)
#
#   -B : border
#        10: write number every 10 degree
#        f5: plot ticks every 5 degree
#
#   -L : plots the map scale
#
#   -W3 : the coast line width is 3 (pts.)
#
#   -I : plot river
#
#   -S : fill the "wet" land with color (e.g. lake)
#
#   -N : plot plotical boundary 1=national 2=state
######

######
#   plot coastline:
######
pscoast $REG $PROJ -P -Df -B1f1 -Na/5/0/0/0 \
-L40.00/28.80/33.40/300 \
-S150/225/255 -V -O -K >> gmt.ps

######
#PLOT FAULTS BY USING FAULTS.car file
######
#psxy FAULTS.car -P $REG $PROJ -O -Sc0.04 -W0/0/255 -G0/0/255 -V -K >> gmt.ps 

######
#   PLOT Numan's Tectonic Divisions
######
#psxy Numan.txt -P $REG $PROJ -O -Sc0.03 -W255/0/0 -G255/0/0 -V -K >> gmt.ps 

#####
#    PLOT BEACH-BALLS
#####
#   Plot Beach-Ball of Focal Mechanism Solutions (FMS) accordinc to -Scscale
#   FMS.txt has solutions of many events. Each solution include:
#   Long Lat Depth Strike-1 Dip-1 Rake-1 Strike-2 Dip-2 Rake-2 Mantissa Exponent NewLong NewLat Title
#   If you have Mo=2.2e+21 dyne-cm, that means Mantissa=2.2 and Exponent=21
#   Note that, you need to modify NewLong and NewLat if you want to place a Beach-Ball in a specific place in the figure.
#   If you want to place a Beach-Ball in the same location type 0 0 or -c insteade of NewLong NewLat.
#   The -: option will intercahnge the order of Long Lat and NewLong NewLat.
#   Title is optional
######
#psmeca FMPAPER.txt -P $REG $PROJ -O -: -Sc1 -G0 -W1p -K -V >> gmt.ps
#psmeca FMPAPER.txt -P $REG $PROJ -O -: -Sc1 -G0 -W1p -CP0.2 -K -V >> gmt.ps
#psvelomeca FMS.txt -P $REG $PROJ -O -: -Sc1 -G0 -W1p -K -V >> gmt.ps
#psvelomeca needs lon lat strike dip rake Mw 

######
#   PLOT STRESS CROSSES 
######
#psvelo STRESS-P.txt $REG $PROJ -Sx0.2 -W3/255/0/0 -V -O -K -: EOF >> gmt.ps

######
#   PLOT EARTHQUAKES
######
#psxy SEISMISITY.txt -P $REG $PROJ -O -: -Sc0.15 -W0 -G255/0/0 -V -K >> gmt.ps 

######
#   plot text: state, city names
######
#       pen positions for text:
#                                (center)
#                                   |
#       (lower right-justified) 11  10  9 (lower left-justified)
#       (middle  "      "    )  7   6   5 (middle   "      "    )
#       (upper   "      "    )  3   2   1 (upper    "      "   )
#
#   column:
#   1=lat 2=lon 3=point size 4=text angle 5=font 6=pen position 7=text
######
pstext $REG $PROJ -V -O -: <<EOF>> gmt.ps
37.50 41.00 18   0.0   1  6 TURKEY
35.00 39.50 20   35.0  1  6 SYRIA
33.00 42.98 22   0.0   1  6 I  R  A  Q
34.00 47.50 22  -40.0  1  6 IRAN
30.00 41.00 16  -30.0  1  6 S A U D I   A R A B I A
29.50 48.00 13.5 0.0   1  6 KUWAIT
32.50 38.50 13   60    11 6 JORDAaN
#36.90 44.60 12 0.0 1 6 1
#36.75 44.30 12 0.0 1 6 2A
#36.40 44.20 12 0.0 1 6 2B
#35.70 43.80 12 0.0 1 6 2C
#33.80 44.00 12 0.0 1 6 3
#34.40 42.60 12 0.0 1 6 3
#34.90 41.80 12 0.0 1 6 3
EOF

exit 0

######
#   plot color topo scale
######

#psscale -D17/4/8/1 -Ccpt.relief -B500 -O -S -K>>gmt.ps

我收到一个语法错误:

sudo sh IRAQ1
grdcut: Syntax error: Must specify only one of the -R, -S or the -Z options
grdcut: Syntax error -R option.  Correct syntax:
    -R<xmin>/<xmax>/<ymin>/<ymax>[/<zmin>/<zmax>]
      Append r if giving lower left and upper right coordinates
    -Rg or -Rd for global domain
    -R<grdfile> to take the domain from a grid file
grdcut: Syntax error -R option.  Correct syntax:
    -R<xmin>/<xmax>/<ymin>/<ymax>[/<zmin>/<zmax>]
      Append r if giving lower left and upper right coordinates
    -Rg or -Rd for global domain
    -R<grdfile> to take the domain from a grid file
grdgradient: Error for input file: No such file (map.grd)
grdgradient: Warning: Option -M is deprecated; -fg was set instead, use this in the future.
grdgradient: Syntax error: Must specify input file
grdimage: Error for input file: No such file (map.grd)
grdimage: Syntax error: Must specify a map projection with the -J option
grdimage: Syntax error: Must specify one (or three) input file(s)
/bin/rm: cannot remove ‘map.grd’: No such file or directory
pscoast: Warning: 5/0/0/0 not a valid number and may not be decoded properly.
pscoast: Error: Your -Dg reference point coordinates require both -R -J to be specified
pscoast: Syntax error: Must specify -R option
pscoast: Syntax error: Must specify a map projection with the -J option
pstext: Syntax error: Must specify -R option
pstext: Syntax error: Must specify a map projection with the -J option

答案1

您的脚本使用了csh语法,因此它不是有效sh脚本。对此的最小修复方法是使用sudo csh而不是sudo sh

但是,csh由于一些原因,它并不是很受欢迎;也许正确的解决方法是用bash(甚至是可移植的 POSIX sh)语法重写你的脚本。

除此之外,set您的脚本中似乎没有任何具体使用csh功能的内容。sh设置变量的语法是

PROJ="-Jm1.4"
REG="-R40.0/49.0/28.0/38.0"

缺少set关键字和等号周围缺少空格都是重大且重要的变化。

还有许多选项错误(我推测)是由于使用了不同版本的程序造成的。我不熟悉这些工具,因此无法建议是否升级、降级或替换它们。切换到csh而不是sh不会改变您可以在这些程序中使用的选项。也许您需要发布一个单独的问题,其中包含更多背景信息——这些脚本是为grdcutgrdgradientgrdimagepscoast等的哪些pstext版本编写的,您现在安装了哪些版本,以及/或者您的脚本实际上试图完成什么。

答案2

我假设你没有运行,csh但是bash。你可以输入来查看你正在运行的 shell 类型echo $SHELL

您可以切换到 C-Shell 以获得与以前一样的所有内容,或者您​​可以调整脚本以适应 Bash。

以下是如何更改 Shell

要在 Bash 中定义变量,不需要关键字set

my_message="hello"
echo "$my_message"

相关内容