LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2018, 07:26 AM   #1
MrKementari
LQ Newbie
 
Registered: Mar 2017
Posts: 4

Rep: Reputation: Disabled
[bin/sh] How to loop over a variable


Hi everybody,

I'm stuck on a simple issue but I can't fix it...
On purpose to monitor filesystems space, I use the following command to get all filesystems :

Code:
myvar=$(df -Pk | egrep "[myFS]")
For example, with /run, I get the following :
Code:
tmpfs               202860    8836    194024       5% /run
tmpfs                 5120       0      5120       0% /run/lock
cgmfs                  100       0       100       0% /run/cgmanager/fs
tmpfs               202860      44    202816       1% /run/user/1000
I would like to get size and name values of each line, so I need to loop through the "myvar" variable, but it's not working...

I tried to do some stuff like :
Code:
while IFS='' read -r line; do
...
done <<< "$myvar"
It works with bin/bash but not with bin/sh... and I have to use bin/sh because target servers don't have bash installed.

Somebody has a solution ?

Thanks a lot.

Edit : Except if it's really needed, I don't want to use temporary files either.

Last edited by MrKementari; 06-15-2018 at 07:41 AM.
 
Old 06-15-2018, 07:59 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Hm. Probably I would try yo use awk/gawk/perl/python/whatever
 
Old 06-15-2018, 08:07 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Here's a little script I've used for years on one server. It checks disk usage on some servers every night.

Code:
for server in $(cat serverlist.txt) ; do
    ssh $server "df -Hl" | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }'  | while read output ; do
        usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
        [ -z "$usep" ] && continue
        partition=$(echo $output | awk '{ print $2 }' )
        if [ "$usep" -ge 90 ]; then
                echo "Running out of space "$partition ($usep%)" on $server as on $(date)"
        fi
    done
done
I can't remember why tmpfs is skipped. It could probably be more elegant and faster. But it works on many different/old/new servers, and some using dash and some using bash.

It outputs the server and partion, so when it's run by cron, I get a mail when some disk is over 90% full. You could probably modify it for your needs.
 
1 members found this post helpful.
Old 06-15-2018, 08:23 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: Then don't use bash-specific features. Try this:
Code:
df -Pk | egrep "[myFS]" | while read Filesystem Total Used Available Capacity MountedOn; do
 ...
done
 
Old 06-15-2018, 08:33 AM   #5
MrKementari
LQ Newbie
 
Registered: Mar 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for all your answers.

I found a way by myself, doing this :

Code:
IFS='
'
for ligne in $var; do
...
done
I set the topic as solved, thanks everybody !
 
Old 06-18-2018, 12:29 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The for list is subject to word splitting (on IFS) and wildcard expansion. You should also disable the latter with
Code:
set -f
The while-read loop, as given in the previous post, is more robust because it reads lines. It can read the line into one variable, or, as demonstrated, it can split the line (on IFS) into variables, that is often useful for further processing. The only disadvantage of piping to while-read is that the while-do-done block runs in a sub shell; defined variables are lost after the block.
 
1 members found this post helpful.
  


Reply



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
[SOLVED] Unable to use variable (i) from loop with FOR in a another variable pedropt Programming 5 12-25-2017 11:31 AM
[SOLVED] Passing variable name to for-do-done loop mackowiakp Linux - Newbie 4 05-22-2017 01:33 PM
variable incrementation in for loop ++nick++ Linux - General 3 10-20-2011 02:03 PM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 04:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 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