Linux script help needed

100 views
Skip to first unread message

Kevin Toppenberg

unread,
Jan 26, 2023, 7:57:41 AM1/26/23
to Hardhats
Hey all, I have two related issues.  I'm going to post them in two threads because the address different problems.

The problem is writing script code that is universal between bash and sh etc.

I have a vistastop script that is run, by the linux system service manager (systemd).  Here is the script:

  1 #!/bin/bash
  2 #vistastop
  3
  4 #---------------------------------------------------------------------------
  5 # K. Toppenberg, MD
  6 # Edited 5/12/21
  7 # This script is called from systemd, as configured by
  8 #   /etc/systemd/system/vista.service
  9 # Output of this script will be output to systemd log file, so no
 10 #  need in here to output to log file directly.
 11 #---------------------------------------------------------------------------
 12
 13 echo "Starting /opt/worldvista/EHR/bin/vistastop.sh"
 14
 15 #The TMG-specific file below will trigger this system
 16 #  to shut down via a crontab script.
 17 #Since we are shutting down, make sure it is gone,
 18 #  so on restart, another shutdown will be avoided
 19
 20 if [ -f /tmp/SHUT_DOWN_REQUEST ] ; then
 21   rm /tmp/SHUT_DOWN_REQUEST
 22 fi
 23
 24 source /opt/worldvista/EHR/etc/env
 25
 26 echo "In vistastart.sh,  gtm_dist=$gtm_dist, vista_home=$vista_home"
 27
 28 $gtm_dist/mumps -run %XCMD "S U=\"^\" D GROUP^ZTMKU(\"SMAN(NODE)\"),GROUP^ZTMKU(\"SSUB
 29
 30 # Wait for TaskMan to stop
 31 echo "Waiting for TaskMan to stop (10 sec)"
 32 sleep 10
 33
 34 processes=$(pgrep mumps)
 35 if [ -n "${processes}" ] ; then
 36   echo "Stopping any remaining M processes nicely"
 37   pgrep mumps | xargs --max-args=1 "mupip" stop
 38   sleep 2
 39 fi
 40
 41 processes=$(pgrep mumps)
 42 if [ -n "${processes}" ] ; then
 43   echo "M process are being shutdown forcefully!"
 44   ps -ef | grep mumps
 45   pkill -9 mumps
 46 fi
 47
 48 rm -fv /tmp/gtm_*
 49
 50 #===========
 51


I think we got this script from someone else and modified it for our purposes.
It is currently working properly through systemd

This morning, my VistA system was hung (see other thread), so I needed to take VistA down and back up.  I could have done this by taking the entire server down, but I thought I could just run the vistastop.sh directly.  I probably should have done this by instructing systemd to restart the service, but I couldn't remember the syntax for that.

So I launched the command via this at the linux prompt:
    kdt@zz:/opt/worldvista/EHR/bin$ sh ./vistastop.sh

Here is the output:

    kdt@zz:/opt/worldvista/EHR/bin$ sh ./vistastop.sh
    Starting /opt/worldvista/EHR/bin/vistastop.sh
    ./vistastop.sh: 24: source: not found
    In vistastart.sh,  gtm_dist=, vista_home=
    ./vistastop.sh: 28: /mumps: not found
    Waiting for TaskMan to stop (10 sec)
    Stopping any remaining M processes nicely
    xargs: mupip: No such file or directory
    M process are being shutdown forcefully!
    kdt     220662       1  0 Jan25 ?        00:03:08 /opt/worldvista/EHR/m/mumps -direct
    kdt     238795       1  0 Jan25 ?        00:02:27 /opt/worldvista/EHR/m/mumps -direct
    kdt     238882       1  0 Jan25 ?        00:02:27 /opt/worldvista/EHR/m/mumps -direct
kdt     238904       1  0 Jan25 ?        00:02:27 /opt/worldvista/EHR/m/mumps -direct
kdt     238943  238938  0 Jan25 ?        00:02:27 /opt/worldvista/EHR/m/mumps -run GTMLNX^XWBTCPM
kdt     336721  336716  0 05:51 ?        00:00:17 /opt/worldvista/EHR/m/mumps -run GTMLNX^XWBTCPM
kdt     354448  354438  0 07:02 pts/1    00:00:00 grep mumps
Thu 26 Jan 2023 07:02:33 AM EST Server stopped.
Done with /opt/worldvista/EHR/bin/vistastop.sh

In the above, I have highlighted 2 errors.  The second one is caused by the first.

Because I wanted the database to be rundown etc, I then manually executed the source command directly from the bash prompt to setup my environment, then launched the script again. Output as below

kdt@zz:/opt/worldvista/EHR/bin$ source /opt/worldvista/EHR/etc/env
kdt@zz:/opt/worldvista/EHR/bin$ sh ./vistastart.sh
Starting /opt/worldivsta/EHR/bin/vistastart.sh
./vistastart.sh: 15: source: not found
In vistastart.sh:
gtm_dist=/opt/worldvista/EHR/m
vista_home=/opt/worldvista/EHR
running as user kdt ()
./vistastart.sh: 23: [: -eq: unexpected operator
Recovering old journals...
%YDB-I-MUJNLSTAT, Initial processing started at Thu Jan 26 07:03:48 2023
%YDB-I-SHMREMOVED, Removed Shared Memory id 5 corresponding to file /opt/worldvista/EHR/g/mumps.dat
%YDB-I-MUJNLSTAT, Backward processing started at Thu Jan 26 07:03:48 2023
%YDB-I-MUJNLSTAT, Before image applying started at Thu Jan 26 07:03:48 2023
%YDB-I-FILERENAME, File /opt/worldvista/EHR/j/mumps.mjl is renamed to /opt/worldvista/EHR/j/mumps.mjl_2023026070348
%YDB-I-MUJNLSTAT, Forward processing started at Thu Jan 26 07:03:48 2023
%YDB-S-JNLSUCCESS, Show successful
%YDB-S-JNLSUCCESS, Verify successful
%YDB-S-JNLSUCCESS, Recover successful
%YDB-I-MUJNLSTAT, End processing at Thu Jan 26 07:03:48 2023
%YDB-I-MUFILRNDWNSUC, File /opt/worldvista/EHR/g/mumps.dat successfully rundown
%YDB-I-FILERENAME, File /opt/worldvista/EHR/j/mumps.mjl is renamed to /opt/worldvista/EHR/j/mumps.mjl_2023026070348_0
%YDB-I-JNLCREATE, Journal file /opt/worldvista/EHR/j/mumps.mjl created for database file /opt/worldvista/EHR/g/mumps.dat with BEFORE_IMAGES
%YDB-I-JNLSTATE, Journaling state for database file /opt/worldvista/EHR/g/mumps.dat is now ON
Starting TaskMan
./vistastart.sh: 50: 1: not found
Thu 26 Jan 2023 07:03:49 AM EST Server started.
Leaving /opt/worldivsta/EHR/bin/vistastart.sh

I highlighted 2 additional errors now encountered.  These make no sense to me because the line numbers don't show the code that is reported as erroneous.    I can't fine "-eq" anywhere in the script.    Here is the environment script "env"

  1 #!/bin/bash
  2 # 5/6/21 ktoppenberg
  3
  4 export vista_instance="EHR" # Like EHR, training, dev.
  5 export vista_user="kdt"
  6 export vista_port="9260"                # Listening port for clients like CPRS.
  7 export vista_home="/opt/worldvista/EHR"
  8 export VH=${vista_home}                 # a short name for paths, etc - temp
  9 export gtm_dist="${VH}/m"
 10 export text_user="text9260"             # id for text users. Default is text$vista_ins
 11 export m2web="/opt/worldvista/EHR/web/m2web"
 12 export PATH="${VH}/m:${PATH}"
 13
 14 export vsb_compliant=1
 15 export vsb_version="0.10"
 16 export GTMXC_md5="$VH/web/m2web/xc/gtm_md5.xc"  # MD5 Library external-call table
 17 #export GTMXC_md5="${VH}/w/xc/gtm_md5.xc"
 18
 19 export gtm_path=$gtm_dist
 20 export gtm_sysid="$vista_owner"
 21 export gtm_vista="$VH"
 22 export gtm_log="$VH/log"
 23 export gtmgbldir="$VH/g/mumps.gld"
 24 export gtmroutines="${VH}/o*(${VH}/p ${VH}/r) $gtm_dist/libgtmutil.so ${gtm_dist}"
 25 export gtm_zinterrupt='I $$JOBEXAM^ZU($ZPOSITION)'
 26 export GTM_REPLICATION=off
 27 export gtm_prompt="yottadb>"
 28
 29 alias rundown='$gtm_dist/mupip rundown -r "*"'
 30 alias GTM="$gtm_dist/mumps -direct"
 31 alias gtm="$gtm_dist/mumps -direct"
 32 alias ydb="$gtm_dist/mumps -direct"
 33 alias mupip="$gtm_dist/mupip"
 34 alias GDE="$gtm_dist/mumps -r ^GDE"
 35 alias gde="$gtm_dist/mumps -r ^GDE"
 36 alias LKE="$gtm_dist/lke"
 37 alias lke="$gtm_dist/lke"
 38 alias DSE="$gtm_dist/dse"
 39 alias dse="$gtm_dist/dse"
 40
 41 #Required due to GT.M change as of 6.0-002A
 42 export gtm_boolean=1
 43 export gtm_side_effects=1
 44 export gtm_zquit_anyway=1
 45
 46 unset VH
 47

From past experience, I think the problem is that I evoked the command via "sh", which the man page indicates is short for "dash", with the following description
DESCRIPTION
     dash is the standard command interpreter for the system.  The current version of
     dash is in the process of being changed to conform with the POSIX 1003.2 and
     1003.2a specifications for the shell.  This version has many features which make
     it appear similar in some respects to the Korn shell, but it is not a Korn shell
     clone (see ksh(1)).  Only features designated by POSIX, plus a few Berkeley exten‐
     sions, are being incorporated into this shell.  This man page is not intended to
     be a tutorial or a complete specification of the shell.

In retrospect, I should have (but didn't) launched the command via:
    kdt@zz:/opt/worldvista/EHR/bin$ ./vistastop.sh

which would have used the #!/bin/bash to execute the script.  But it is an ingrained habit of mine to use:  sh /<scriptname.sh>

Question:
1) Is there a universal way to achieve the source command that will work in both bash and sh?
2) Any ideas about the "-eq" error.  And what is the universal way to do this?

Any other thoughts?

Thanks

Kevin T




Kevin Toppenberg

unread,
Jan 26, 2023, 8:00:09 AM1/26/23
to Hardhats
Reviewing my post, apparently the google group strips out the highlighting I included during composition.  The errors won't pop via yellow background.  Sorry.

KT

Sam Habiel

unread,
Jan 26, 2023, 8:12:50 AM1/26/23
to hard...@googlegroups.com
Kevin,

Unless you just want to suffer, invoke your scripts directly (they
need to have the x bit) and it will use the default interpreter. You
told your script to use sh explicitly.

--Sam
> --
> --
> http://groups.google.com/group/Hardhats
> To unsubscribe, send email to Hardhats+u...@googlegroups.com
>
> ---
> You received this message because you are subscribed to the Google Groups "Hardhats" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to hardhats+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/hardhats/1b1ccba9-f503-4a35-9319-3e699203e9d4n%40googlegroups.com.

David Wicksell

unread,
Jan 26, 2023, 1:01:43 PM1/26/23
to Hardhats
Hi Kevin,

1) The . operator is equivalent to source and should work in Bash, Dash, Bourne, etc. So just use `. /opt/worldvista/EHR/etc/env`
2) The `-eq` error is coming from vistastart.sh. You didn't share the code for that script. But if I had to guess, I'd guess you
     have a `==` where a `=` should be used, or where an `-eq` should be used. Only Bash supports `==` for equality comparisons,
     and you use `-eq` for number comparison, and `=` for string comparison.

I hope that helps.

David Wicksell
Fourth Watch Software LC

Kevin Toppenberg

unread,
Jan 26, 2023, 3:46:08 PM1/26/23
to Hardhats
David,

Thanks for this help.  I'll work on these changes.  :-)

Kevin

Kevin Toppenberg

unread,
Jan 26, 2023, 4:02:39 PM1/26/23
to Hardhats
Thinking more about this, I looked at vistastart.sh.  Thanks David for catching that I was looking in the wrong script.  I'll include the full script below. 

Here is the line that generated the error:

23 if [ $EUID -eq 0 ]; then
 24   echo "Run as $vista_user, not as root."
 25   exit;
 26 fi

The error line was:
./vistastart.sh: 23: [: -eq: unexpected operator

I found this reference comparing '=' '==' and '-eq'

----------------------------------------------------

    = and == are for string comparisons
    -eq is for numeric comparisons
    -eq is in the same family as -lt, -le, -gt, -ge, and -ne

   == is specific to bash (not present in sh (Bourne shell), ...). Using POSIX = is preferred for compatibility. In bash the two are equivalent, and in sh = is the only one that will work.

----------------------------------------------------


So the first answer (above) reports that with sh, only the '=' works.  ... But I'm not sure this is true.  I made a small script to test this.

  1 #!/bin/bash
  2
  3 if [ 1 -eq 1 ]; then
  4   echo "equals!"
  5 fi
  6
  7 if [ $EUID -eq 0 ]; then
  8   echo "equals!"
  9 fi
 10

Here is the output:
kdt@zz:/tmp$ sh kill.sh
equals!
kill.sh: 7: [: -eq: unexpected operator
kdt@zz:/tmp$ ./kill.sh
equals!
kd@zz:/tmp$

So, any suggestion on better syntax?

Thanks again!!

Kevin T


Script below....

#!/bin/bash
  2 # vistastart

  3
  4 #---------------------------------------------------------------------------
  5 # K. Toppenberg, MD
  6 # Edited 5/12/21
  7 # This script is called from systemd, as configured by
  8 #   /etc/systemd/system/vista.service
  9 # Output of this script will be output to systemd log file, so no
 10 #  need in here to output to log file directly.
 11 # This script should be called as user kdt0p, NOT ROOT
 12 #---------------------------------------------------------------------------
 13 echo "Starting /opt/worldivsta/EHR/bin/vistastart.sh"
 14
 15 source /opt/worldvista/EHR/etc/env
 16
 17 #cd `dirname $0`
 18 echo "In vistastart.sh:"
 19 echo "gtm_dist=$gtm_dist"
 20 echo "vista_home=$vista_home"
 21 echo "running as user $USER ($EUID)"
 22
 23 if [ $EUID -eq 0 ]; then
 24   echo "Run as $vista_user, not as root."
 25   exit;
 26 fi
 27
 28 #rm -f ../tmp/*.mj[oe]
 29 rm -f /home/kdt0p/*.mj[oe]
 30
 31
 32 # If a database is shutdown cleanly there shouldn't be anything in the
 33 # journals to replay, so we can run this without worry
 34
 35 if [ -f ${vista_home}/j/mumps.mjl ]; then
 36   echo "Recovering old journals..."
 37   $gtm_dist/mupip journal -recover -backward $vista_home/j/mumps.mjl
 38 fi
 39 $gtm_dist/mupip rundown -region DEFAULT
 40 $gtm_dist/mupip set -journal="enable,on,before,f=$vista_home/j/mumps.mjl" -file $vista_home/g/mumps.dat
 41
 42 echo "Starting TaskMan"
 43 $gtm_path/mumps -run ZTMB
 44 #$gtm_path/mumps -direct <<EOF
 45 #D RESTART^ZTMB
 46 #y
 47 #EOF
 48
 49 #remove old journal files
 50 if (( $(find ${vista_home}/j -name '*_*' -mtime +3 -print | wc -l) > 0 )); then
 51   echo "Deleting old journals..."
 52   find ${vista_home}/j -name '*_*' -mtime +3 -print -delete
 53 fi
 54 #python vista_rm_journals.py >> $vista_path/log/vista_server.log
 55
 56 echo "$(date) Server started."
 57 echo "Leaving /opt/worldivsta/EHR/bin/vistastart.sh"
 58
 59
 60

David Wicksell

unread,
Jan 26, 2023, 4:32:22 PM1/26/23
to Hardhats
Hi Kevin,

 I'm assuming that you want this to work in Bourne and Dash, and not just Bash. So my answer reflects that, and I
won't be discussing Bash specific things like using `[[ ... ]]` instead of `[ ... ]`. That out of the way, you almost certainly
have an empty environment variable with $EUID. So there are a couple things you can do to fix that, from the perspective
of having a portable, robust script that handles things like unset variables. Here are the basic things you can do:

1) Test that $EUID is defined with `if [ -n "$EUID" ] ...` (note that the double quotes around $EUID are necessary here),
     and only if it is defined, go on to your test on line 7.

2) Give your environment variable a default number like `${EUID:--1}` (the :- specifies what comes next is a default if the
     variable is not defined, in this case a -1).

3) Change it to a string comparison with = instead of -eq, and put double quotes around the variable, and the test value, so
     that if it is undefined it is compared as the empty string, like `if [ "$EUID" = "0" ] ...` (this can catch you with more
     complex comparisons though).

 The error is occurring because you are doing a numeric comparison, but with $EUID undefined, you only have a number on
one side of the comparison, and that isn't allowed. I hope this helps.

David Wicksell
Fourth Watch Software LC

David Wicksell

unread,
Jan 26, 2023, 4:36:38 PM1/26/23
to Hardhats
Err. line 23, not 7.

Kevin Toppenberg

unread,
Jan 26, 2023, 8:03:48 PM1/26/23
to Hardhats
This was very helpful.  Thanks!  I think I will use option #3.

:-)

Kevin

ivaldes

unread,
Jan 27, 2023, 1:39:51 PM1/27/23
to Hardhats
Here is mine/Astros for what it is worth. The start script is noteworthy for rebuilding the GDM file. I would like to turn journaling off because I use cloud backup now but I have developed an allergy to the complexity of syntax I built many years ago.

#!/bin/bash
#vistastop
#!/bin/bash
cd `dirname $0`
echo "pwd: `pwd`"
source /opt/astrovista/EHR/etc/env.astrovista
cd $vista_path/bin
./run.sh STOP^ZTMKU <<EOF
y
y
h
EOF
sleep 1
ps -ef | grep mumps | grep -v grep | awk '{print $2}' | xargs kill 2>/dev/null
echo "`date` $tmp_date Server stop." >> $vista_path/log/vista_server.log


#!/bin/bash
# vistastart
#!/bin/bash
cd `dirname $0`

if [ $EUID -eq 0 ]; then
  echo "Run as $vista_user, not as root."
  exit;
fi
rm -f ../tmp/*.mj[oe]
source /opt/astrovista/EHR/etc/env.astrovista
echo "vistastart gtm_dist: $gtm_dist, gtm_vista: $gtm_vista"
$gtm_dist/mupip journal -recover -backward $gtm_vista/journals/mumps.mjl
$gtm_dist/mupip rundown -region DEFAULT

rm -f $vista_path/globals/scratch.dat
rm -f $vista_path/globals/mumps.gld
echo "before GDE"

# Merged similar Astronaut install script stuff and the below about 20 lines with Medsphere Systems Corp.
# instanceadd script with Astronaut variable modifications and a few changes.
# Per Gus, allocation count resulting in 2Gb and extension count 0 is better because you will fill allocation count resulting in 2Gb and extension count 0 is better because you will fill
# up your database before you fill up your disk.
(
    umask 027
#$gtm_path/mumps -run ^GDE <<EOF
$gtm_path/mumps -run ^GDE > /dev/null 2>&1 <<EOF
CHANGE -SEGMENT DEFAULT -ACCESS_METHOD=BG -BLOCK_SIZE=4096 -ALLOCATION=500000 -EXTENSION_COUNT=0 -GLOBAL_BUFFER_COUNT=4096 -LOCK_SPACE=400 -FILE=$vista_path/globals/mumps.dat
CHANGE -REGION DEFAULT -RECORD_SIZE=4080 -KEY_SIZE=255 -JOURNAL=(BEFORE_IMAGE,FILE_NAME="$vista_path/journals/mumps.mjl") -DYNAMIC_SEGMENT=DEFAULT
TEMPLATE -SEGMENT -ACCESS_METHOD=BG -BLOCK_SIZE=4096 -ALLOCATION=1024 -EXTENSION_COUNT=1024 -GLOBAL_BUFFER_COUNT=4096 -LOCK_SPACE=400
TEMPLATE -REGION -RECORD_SIZE=4080 -KEY_SIZE=255 -NOJOURNAL
ADD -SEGMENT SCRATCH -FILE=$vista_path/globals/scratch.dat
ADD -REGION SCRATCH -DYNAMIC_SEGMENT=SCRATCH
ADD -NAME TMP -REGION=SCRATCH
ADD -NAME TEMP -REGION=SCRATCH
ADD -NAME UTILITY -REGION=SCRATCH
TEMPLATE -REGION -RECORD_SIZE=4080 -KEY_SIZE=255 -JOURNAL=BEFORE_IMAGE
EXIT
EOF
)

# mumps doesn't exit non-zero even if something goes wrong in ^GDE, so look for
# the mumps.gld to determine success
[ -f "$vista_path/globals/mumps.gld" ] ||
exit_with_error "$vista_path/instanceadd.sh" "Unable to create global directory file"

mv $vista_path/globals/mumps.dat $vista_path/globals/mumps.tmp
$gtm_path/mupip create
rm -f $vista_path/globals/mumps.dat
mv $vista_path/globals/mumps.tmp $vista_path/globals/mumps.dat

[ -f "$vista_path/globals/scratch.dat" ] ||
exit_with_error "$vista_path/instanceadd.sh" "Unable to create scratch.dat file"

chmod 760 $vista_path/globals/scratch.dat

#echo "before .1 zosf."
$gtm_dist/mumps -direct > /dev/null 2>&1 << EOF
SET ^%ZOSF("TMP")="$vista_path/bin/tmp"
h
EOF

$gtm_dist/mupip set -journal=enable,on,before,filename=$vista_path/journals/mumps.mjl -region DEFAULT
#$gtm_dist/mupip set -journal="enable,on,before" -file $gtm_vista/globals/mumps.dat

echo "before box:volume"
# Set box:volume pair.
#$gtm_path/mumps -r ^VWSTART <<EOF
$gtm_path/mumps -r ^VWSTART > /dev/null 2>&1 <<EOF
XUTIME
EOF

#$gtm_path/mumps -direct  > /dev/null 2>&1 <<EOF
$gtm_path/mumps -direct <<EOF
S DUZ=1
D RESTART^ZTMB
y
EOF

find $gtm_vista/globals -iname mumps.mjl_\* -mtime +3 -exec rm -v {} \;
echo "`date` $tmp_date Server start." >> $vista_path/log/vista_server.log

K.S. Bhaskar

unread,
Jan 27, 2023, 4:20:58 PM1/27/23
to Hardhats
In production, run without journaling only those regions whose data you can live with losing. Period.

Regards
– Bhaskar

shane dewitt

unread,
Jan 28, 2023, 6:51:06 PM1/28/23
to Hardhats
Dash shell doesn't have UID or EUID.

% echo UID = $UID EUID = $EUID
UID = 501 EUID = 501
% /bin/dash
$ echo UID = $UID EUID = $EUID
UID = EUID =

Try /bin/id -u for EUID.

shane dewitt

unread,
Jan 28, 2023, 6:51:13 PM1/28/23
to hard...@googlegroups.com
Apparently UID and EUID are bash only.


Maybe try the “id” command.

Sent from my iPhone

On Jan 26, 2023, at 4:36 PM, David Wicksell <dlwic...@gmail.com> wrote:


--
--
http://groups.google.com/group/Hardhats
To unsubscribe, send email to Hardhats+u...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups "Hardhats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hardhats+u...@googlegroups.com.

Kevin Toppenberg

unread,
Jan 29, 2023, 9:20:40 AM1/29/23
to Hardhats
Shane,

Your post was very helpful  Thank you!  I liked that link you included.

Kevin
Reply all
Reply to author
Forward
0 new messages