LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-22-2016, 01:14 AM   #1
hiten11
LQ Newbie
 
Registered: Oct 2016
Posts: 4

Rep: Reputation: Disabled
Smile Linux Shell script not working on SunOs 5.10


Hi All,

I have made a script that will output filesystem used space % using df -h command.Kindly find the script below

Code:
#!/bin/bash
Partition=($(df -h |grep ^/ | awk '{print$1}'))
Used=($(df -Pk |grep ^/ |awk '{print$5}' | sed "s/[^0-9]//g"))
for ((i=0; i<${#Partition[*]}; i++));
 do
   echo "File System ${Partition[i]}used space is ${Used[i]}"
 done
But the same is not working on "SunOS 5.10 Generic_150400-29 sun4v sparc SUNW,T5440"

Can Anyone help me to achieve similar output in Solaris 5.10 too
 
Old 10-22-2016, 01:47 AM   #2
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Location: Non. Je suis propriétaire – No. I am an owner.
Distribution: Apple-selling shops, markets and direct marketing
Posts: 1,493
Blog Entries: 39

Rep: Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774
Quote:
Originally Posted by hiten11 View Post
But the same is not working on "SunOS 5.10 Generic_150400-29 sun4v sparc SUNW,T5440"
Can Anyone help me to achieve similar output in Solaris 5.10 too
I see, that you are pragmatic, but as some of the people on LQ are geniuses without even knowing your Solaris-system, it could help to give us an error-message or other detail which may hint at the origin of your observed incompatibility.

Would it not be great to just make your own script work, instead of being blessed with a free, working Solaris-specific solution?
 
Old 10-22-2016, 01:58 AM   #3
hiten11
LQ Newbie
 
Registered: Oct 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi Micheal,

Sorry for that , i want to make a script that will echo used space across all partitions that are visible in df -k command.In the first line


Partition=($(df -h |grep ^/ | awk '{print$1}'))

I am parsing all the available filesystem in "Partition" array but am not able to achieve the same in SunOs, same code is working well in Linux.

For rest of the lines that i have posted , i myself have found out a workaround.It would be great if you could help me out in parsing available Filesystem in an array.
 
Old 10-22-2016, 02:02 AM   #4
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Location: Non. Je suis propriétaire – No. I am an owner.
Distribution: Apple-selling shops, markets and direct marketing
Posts: 1,493
Blog Entries: 39

Rep: Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774
Okay, this might become awkward, as I do not have a Solaris-system at my disposition.
My question is this: When you run your script on Solaris, WHAT HAPPENS ??
 
Old 10-22-2016, 02:49 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,903
Blog Entries: 3

Rep: Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585
In addition to the exact error message, there are some things to check. One is be sure that "df -h" on both systems create similar output. Run them outside of the script and compare them manually. Another is removing the redundant "grep", since "awk" can do some pattern matching:

Code:
Partition=($(df -h | awk '/^\// {print$1}'))
Also, why do you have the extra parenthesis? They change what $Partition gets filled with.
 
Old 10-22-2016, 06:31 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
Having worked with Solaris only a little bit, my questions would be:

1. Does it have bash? If so, what version? (none that I have worked on did)

2. Does it have awk? If so, what version?

3. Does it have df? If so, are all the switches applicable?


You can probably see where this is going. on top of what the others have already asked about error messages, when you are on a completely different type of system you need to provide a lot more information as
most of us have either not worked with your system type or do not currently have access to one so we cannot replicate your issues.
 
1 members found this post helpful.
Old 10-22-2016, 06:57 AM   #7
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 4,924

Rep: Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402Reputation: 2402
Quote:
Originally Posted by grail View Post
Having worked with Solaris only a little bit, my questions would be:

1. Does it have bash? If so, what version? (none that I have worked on did)

2. Does it have awk? If so, what version?

3. Does it have df? If so, are all the switches applicable?


You can probably see where this is going. on top of what the others have already asked about error messages, when you are on a completely different type of system you need to provide a lot more information as
most of us have either not worked with your system type or do not currently have access to one so we cannot replicate your issues.
Good advice! And, if I may add, entire books have been written on portability. You appear to have read none of them, and seeking some for some reading might be wise.
You are using multiple tools for this and on an operating system where NONE of them are sure to be GNU standard! Generally, a portable script will use /bin/sh and only relatively posix standard shell features. Called utilities will use only the most standard features, and as few of those as can be managed. Even then, conditional execution based upon detected differences may be required.

I like what you are doing, not for utility but as a learning exercise. This is a perfect setup for a lesson in portability.Please let us know the results of your research and testing.
 
Old 10-22-2016, 07:37 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,258

Rep: Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207
compatibility is the issue here -- yes that is it have you tried looking up how to write a script SunOS to find the differences between the two?
Apples MAC too is a UNIX based system but I bet it too has its quirks when it comes to running bash commands, even Solaris too would fall into this pit.


bash
Quote:

/bin/sh on Solaris is a POSIX complaint bourne shell. If you have written bash-centric scripts, replace #!/bin/sh with #!/bin/bash
or visa versa as the case maybe. says that web page
 
Old 10-22-2016, 09:37 AM   #9
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
There are usually two versions of awk on Sun systems, oawk and nawk (old and new). oawk is traditional AWK and does not work the same as nawk, which is the current version being maintained by Brian Kernighan at his Princeton web site. /usr/bin/awk is a symbolic link to either oawk or nawk, typically oawk because some utilities have not been updated to use nawk. Perhaps you can check and see if there are different results (modify awk to oawk then the to nawk)?

If you're interested in nawk, which, in my opinion, is a better alternative to gawk on Linux systems, you can download the source from http://www.cs.princeton.edu/~bwk/ and compile it on your Linux system (it compiles to an a.out, you copy that to, say, /usr/local/bin/nawk.

Hope this helps some.
 
Old 10-24-2016, 12:29 AM   #10
hiten11
LQ Newbie
 
Registered: Oct 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi All,

Sorry for delayed reply .

When i am executing below command in SunOS

set -A Partition ( df -k | nawk -F' ' '{print $1}' )

I am getting error "bash: syntax error near unexpected token `('"

So am unable to store output in an array but when i am executing command i am getting output as list of all available filesystem

Filesystem
/
/appuser
/bocmsdata
/ctmag
/customlog
/dev
/fin62
/fin7012
/fin7014
/igrsdata
/oem10g
/ora11204
/ora12c
/oracle7 etc.

So kindly help me to get each of the above values in an array
 
Old 10-24-2016, 01:03 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,903
Blog Entries: 3

Rep: Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585Reputation: 3585
I can't find my old OpenSolaris discs and Oracle's download function is *%$#^&* broken, perhaps on purpose. I never found them worth the money when I have encountered them in the past, anyway. And given their priorities, acquiring Solaris is only trouble. I notice that most of the OpenSolaris derivatives available outside of Oracle have mostly defaults of using the GNU userland. But I digress.

Presumably, if you are using bash, you should use the syntax for GNU Bash instead of for one of the Solaris shells. You'll need to make use of command substitution though. $( ... )

Code:
partition=( $( df -k | nawk -F' ' '{print $1}' ) )
echo ${partition[*]}
 
Old 10-24-2016, 02:01 AM   #12
hiten11
LQ Newbie
 
Registered: Oct 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Hi I tried the above command but the execution of this command is not getting completed successfully and i have to explicitly press ctrl^c to kill the command

Kindly find attached the snap
Attached Thumbnails
Click image for larger version

Name:	Snap.png
Views:	23
Size:	1.6 KB
ID:	23362  
 
Old 10-24-2016, 02:07 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,252

Rep: Reputation: 6837Reputation: 6837Reputation: 6837Reputation: 6837Reputation: 6837Reputation: 6837Reputation: 6837Reputation: 6837Reputation: 6837Reputation: 6837Reputation: 6837
do not use ( ):
Code:
partition="$( df -k | nawk -F' ' '{print $1}' )"
and as it was already mentioned please post not only the command you tried but the result as it was displayed. Exactly! Saying "not getting completed successfully" will not help anyone to give you any help
 
Old 10-24-2016, 05:29 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
Well the OP did want an array so I see why the extra () are needed. Maybe we could take a step back now that we have (sort of) identified answers to some questions (although indirectly).

What is the output for the following:
Code:
df -k

df -k | nawk '{print $1}'

df -k | nawk -F' ' '{print $1}'
Obviously if there are issues with any of the above, then we can see about putting it in an array.

I would mention that the nawk command being used does not match any of the previous awk examples.
 
Old 10-24-2016, 07:52 AM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,258

Rep: Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207Reputation: 2207
yeah I think everybody pretty much covered it, it is your syntax
Quote:
set -A Partition="$( df -k | nawk -F' ' '{print $1}' )"
now if that does not work then it is you again????

in my terminal
Code:
userx@SlackDaddy & ~ >> $Partition="$( df -k | nawk -F' ' '{print $1}' )"
bash: nawk: command not found

(changed it to awk instead)

userx@SlackDaddy & ~ >> $Partition="$( df -k | awk -F' ' '{print $1}' )"

userx@SlackDaddy & ~ >> $echo $Partition
Filesystem /dev/root devtmpfs tmpfs tmpfs cgroup_root /dev/sdb4 /dev/sda3 /dev/sdb1 cgmfs
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getopts script not working in redhat sh (works in sunos sh) khandu Programming 1 02-16-2012 08:27 AM
password less authentication from SunOS to HP-UX not working xxx_anuj_xxx Solaris / OpenSolaris 4 07-29-2009 11:10 AM
Shell scripts migration from SunOS 5.9 to Red Hat Linux Advanced Server 4.0 64-bit devenr_2000 Linux - Newbie 1 11-13-2007 12:08 PM
SunOS 5.8 Retrieve the shell handle from another widget? fuzelogic Programming 1 03-08-2004 07:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration