0

I am new to shell scripting and solaris operating systems. I want to write a script that monitors disk usage. Following some tutorials online I got started with it. As a first step I tried to assign the output of a command to a variable and echo it.

#!/bin/sh

CURRENT=$(df -h /ccviews1|sed -n '2p'|awk '{print $5 }'|sed 's/%//g')
echo $CURRENT

When I run the script, it is giving below error

./diskMonitor.sh: syntax error at line 3: `CURRENT=$' unexpected

Please help me with this issue. Also, I would like to if writing shell scripts in Ubuntu and writing in Solaris are same. If there is any difference, how should I proceed?

3
  • What version of Solaris are you using? Earlier versions of Solaris have /bin/sh as the Bourne Shell.
    – user14755
    Aug 18, 2016 at 7:19
  • Try #!/bin/bash instead.
    – schaiba
    Aug 18, 2016 at 7:28
  • /bin/sh is not Bash. GNU/Linux conflates the two. Aug 21, 2016 at 0:39

1 Answer 1

1

If you are using Solaris 10 and older, you need to adjust your script one of these ways:

Portable to the legacy Bourne shell:

CURRENT=`df -h /ccviews1|sed -n '2p'|awk '{print $5 }'|sed 's/%//g'`
echo $CURRENT

Use a POSIX shell

#!/usr/xpg4/bin/sh
PATH=$(getconf PATH):$PATH
CURRENT=$(df -h /ccviews1|sed -n '2p'|awk '{print $5 }'|sed 's/%//g')
echo $CURRENT

Use Solaris mainstream shell (bash will work too here)

#!/bin/ksh

CURRENT=$(df -h /ccviews1|sed -n '2p'|nawk '{print $5 }'|sed 's/%//g')
echo $CURRENT

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .