UNIX/Shell function does not work as wished


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers UNIX/Shell function does not work as wished
# 1  
Old 08-07-2017
UNIX/Shell function does not work as wished

Hello everyone
I really hope you can help me, I can't continue:
Im on a project to work with my Server.
I wanted to put on my server all data-systems and I did this:

Code:
df -h

The output is a string.

How can I turn the string into a table?
Code:
Dateisystem Größe Benutzt Verf. Verw% Eingehängt auf udev 1.8G 0 1.8G 0% /dev tmpfs 361M 11M 350M 3% /run /dev/sda1 28G 8.1G 18G 32% / tmpfs 1.8G 44K 1.8G 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup anonymuser 219G 121G 98G 56% /media/anonymuser tmpfs 361M 152K 360M 1% /run/user/1000

On the second point I wanted to see all java processes and runned this command:

Code:
 ps -ef | grep java

Now the output has many unneccesary informations and I just want to have the CPU consumption.
How can I get the CPU consumption of each java process?
# 2  
Old 08-07-2017
Welcome to the forum.
(and thanks for using code tags correctly in your very first post!)

The output of df -h certainly is NOT a string but a few lines of text. I guess you assigned it to a shell variable; when printing that WITHOUT proper quoting / escaping the shell (which you fail to mention) will convert all line feeds into spaces and make it seem like a single string. Try printing using double quotes.

What do you mean by "table"? DB table, HTML table, spread sheet table, text file looking like a table?

For your second question, look into man ps for OUTPUT FORMAT CONTROL and STANDARD FORMAT SPECIFIERS; in your case %cpu might help.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-07-2017
Hello RudiC
Nice for seeing such a nice community here.

In my case, I want a basic table like a HTML.
Sorry for not being so exactly.

And for the other point.
Thank you a thousand times, I will check this out.

---------- Post updated at 04:13 AM ---------- Previous update was at 04:11 AM ----------

Quote:
Originally Posted by RudiC
Welcome to the forum.
(and thanks for using code tags correctly in your very first post!)

The output of df -h certainly is NOT a string but a few lines of text. I guess you assigned it to a shell variable; when printing that WITHOUT proper quoting / escaping the shell (which you fail to mention) will convert all line feeds into spaces and make it seem like a single string. Try printing using double quotes.

What do you mean by "table"? DB table, HTML table, spread sheet table, text file looking like a table?

For your second question, look into man ps for OUTPUT FORMAT CONTROL and STANDARD FORMAT SPECIFIERS; in your case %cpu might help.
Hello RudiC
Nice for seeing such a nice community here.

In my case, I want a basic table like a HTML.
Sorry for not being so exactly.

And for the other point.
Thank you a thousand times, I will check this out.
# 4  
Old 08-07-2017
Try this site's search function for e.g. "HTML table" for posts in the last months and the links in the lower left therein, e.g. converting-text-file-html and converting-shell-script-html.
# 5  
Old 08-07-2017
It is also worth looking to see if your system has pgrep and pkill.

Andrew
# 6  
Old 08-07-2017
Quote:
Originally Posted by anonymuser
I wanted to put on my server all data-systems and I did this:

Code:
df -h

The output is a string.

How can I turn the string into a table?
As RudiC already told you the output is not a string. Try the command on a command line in your terminal and you will see a nicely formatted (ASCII)-table.

The answer to your question is simple: use a shell script. I assume (because your interest in Java processes) that you have some programming background and the good news is: all the common UNIX/Linux shells have a twofold purpose. They can be used to enter commands interactively but they are also (quite powerful) language interpreters for the programming language they implement. If you have a Windows background: think of cmd.exe and powershell rolled into one.

To implement a script for your filesystems, you might want to start with selecting a better option set to df: the command you used is optimized for human reading (hence the option -h), which is not optimal for machine reading. As you might have noticed there are different units used for filesystem sizes and parsing (K)ilobytes, (M)egabytes, (G)igabytes and whatnot will make your code more complicated.

Using the -P (POSIX) switch will make that easier because only one unit (1024-byte blocks) will be used. Furthermore, POSIX is *the* UNIX standard and this way you will be able to use your script on a wide variety of UNIX systems whereas -h is only available on Linux-systems and if you use it your script will only run on Linux-systems.

Now try the command on your terminal and look at the output. Here is an example of the laptop i am writing this:

Code:
# df -P
Filesystem     1024-blocks    Used Available Capacity Mounted on
udev              16383972       0  16383972       0% /dev
tmpfs              3281216    9532   3271684       1% /run
/dev/sda6         19553560 6880668  11656572      38% /
tmpfs             16406076     284  16405792       1% /dev/shm
tmpfs                 5120       4      5116       1% /run/lock
tmpfs             16406076       0  16406076       0% /sys/fs/cgroup
/dev/sda4         20511356  407184  19039212       3% /opt/games
/dev/sda7         19553560   44992  18492248       1% /altroot
/dev/sda3         50385596 6707112  41095956      15% /opt/images
/dev/sda1           474730  135491    310209      31% /boot
/dev/sda8         49082176 2452096  44113756       6% /home
tmpfs              3281216      36   3281180       1% /run/user/1000

You first need to analyse which part of the information you are interested in: probably not the first line of output because this is header information. Maybe also not in filesystems of the type "tmpfs" or "udev" because these are not real filesystems, they are only created at runtime. You can filter lines with the grep command (i suggest you read the man page for it, also try the command and play around with it by changing it and observing the different outcomes), so let us try this:

Code:
# df -P | grep -ve tmpfs -ve udev -ve Filesystem
/dev/sda6         19553560 6880672  11656568      38% /
/dev/sda4         20511356  407184  19039212       3% /opt/games
/dev/sda7         19553560   44992  18492248       1% /altroot
/dev/sda3         50385596 6707112  41095956      15% /opt/images
/dev/sda1           474730  135491    310209      31% /boot
/dev/sda8         49082176 2452104  44113748       6% /home

OK, this is is what we are really interested in. Now we need to read the columns one by one and assign the values to variables which we will later be able to process. For this there is the shell built-in command read. The shell will split the input along whitespace itself so we don't have to do this ourselves. For instance:

Code:
# df -P | grep -ve tmpfs -ve udev -ve Filesystem | while read device size used avail percent mount ; do
     echo MOUNT: $mount
     echo "    device...: $device"
     echo "    size.....: $size"
     echo "    used.....: $used (${percent})"
     echo "    available: $avail"
     echo ""
done

As you see commands can span several lines and you still can enter them on the command line. You can also save this to a file and execute it - this is a "script"! There is no difference between what you enter on the command line and what you write in scripts - everything you use on the command line can be used in a script and vice versa.

A few things in the above command need to be explained:

Code:
read var1 var2 var3 ....

this command reads a line of input (in the case above provided by the directing the output of the previous command into it, a so-called "pipeline") and splits it along whitespace. The first "word" then goes to a variable called "var1", the second word to "var2" and so on. If there are more words than variables the last variable gets all the remaining words, if there are more variables than words the last variables will be empty. In our case there are exactly as many variables as there are words and each variable gets one word.

Code:
while command ; do
    commands ....
done

This is a loop which is repeated as long as command (which can be any command or built-in, in our case it is the read) returns a return code of 0. Every command, when ending, sets such a return code and UNIX convention is that a return code of 0 means "success", everything else some sort of failure.

In our case the read will return 0 as long as there are input lines to read, only when there is nothing to read any more it will return non-0, which will terminate the while-loop.

A last thing about variables: when you assign them you do that:

Code:
var=content

but when you want to use them (i.e. to display them) you need to put a "$" before their name like this:

Code:
echo $var

you can also surround the output of a variable by fixed text:

Code:
# var=mycontent
# echo $var
mycontent
echo ==$var==
==mycontent==

but in some cases where the fixed text will be characters it might be ambiguous:

Code:
# var=mycontent
# echo foo$varbar
??

This would display "foo" followed by the content of the variable varbar instead of the variable var followed by the fixed text "bar". TO remove such ambiguities use the braces:

Code:
# var=mycontent
# echo foo${var}bar
foomycontentbar

Now, this is a rather lame excuse for a good introduction into shell programming, but i hope i have piqued your interest enough to make you start scripting. Have fun with the shell!

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX: passing stuff to a shell function

I have users that print files to selected printers. Instead of creating one function for each printer I would like to have just one and passing the files to print as well as the wanted printer. The following code does not work, of course. I'm expecting that $1 is the list of files to be printed... (6 Replies)
Discussion started by: emare
6 Replies

2. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

3. Shell Programming and Scripting

UNIX Shell script to work with .xml file

Hi Team, Could you please help me on below query: I want to retrieve XML elements from one .xml file. This .xml file has commented tags as well. so i am planning to write Unix command/script which 1.will chekc for this .xml file 2. it will ignore the commented XML lines. i.e. XML tags between... (3 Replies)
Discussion started by: waiting4u
3 Replies

4. Shell Programming and Scripting

Function doesn't work

Hello, and here's my problem: I can't get my function to do what I want. When I call my function get_from_A_to_F I give it an argument $remainder. I want my function to substitute a number higher than 9 to a specific letter. If the argument is equal to 10 than it should change it to "A".... (8 Replies)
Discussion started by: linas
8 Replies

5. Shell Programming and Scripting

How the Sleep function will work?

Hi All, I am new to Unix , there i am facing one problem with sleep command. that is .. in while loop i have defined sleep function .. my condition is like this while #i knew this is infinite loop do sleep 200 echo "hello " done. this condition will never become .. true... (3 Replies)
Discussion started by: mandlysreedhar
3 Replies

6. UNIX for Dummies Questions & Answers

copy *. does not work in function

Hi, I want to copy a file/directory ( recursively , if needed) and if destination directory does not exist create it ( with parent directory, if needed). funcopy () { if ; then echo "$2 exists , copying files" cp -r "$1" "$2" else echo "Directory does not exist;Create directory" mkdir... (1 Reply)
Discussion started by: greet_sed
1 Replies

7. Shell Programming and Scripting

Cannot get grep to work within function.

Hello again, Am having an issue now with getting a simple grep command to work within a function.. The function is as below... function findRecord() { output=grep "001" recordDatabase echo $output } At the moment the "001"... (3 Replies)
Discussion started by: U_C_Dispatj
3 Replies

8. UNIX for Dummies Questions & Answers

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (5 Replies)
Discussion started by: csrazdan
5 Replies

9. Shell Programming and Scripting

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (1 Reply)
Discussion started by: csrazdan
1 Replies

10. Shell Programming and Scripting

Will the continue function work ????

I have written a script which does a following functions:- 1) Check a area if it is mounted or not 2) If the area is not mounted it will prompt the user to mount the are. 3) Once the area is mounted and the option is given as Y or y the script continues... My question is will the below... (2 Replies)
Discussion started by: kamlesh_p
2 Replies
Login or Register to Ask a Question