How can I use loop variable in array?

onur

I have a script for writing to disk usage to csv file like this:

#!/bin/bash

disklist=(
/
)
[ -f "disk.conf" ] && disklist=($(<disk.conf))   

datesed=$(date +"%d\/%m\/%Y")

df -P "${disklist[@]}" | sed -ne 's/^.* \([0-9]\+\)% \(.*\)$/'$datesed', \2, \1%/p' >> disk.csv

Output like this:

11/06/2015, /, 18%
11/06/2015, /dev, 1%
11/06/2015, /bin, 5%

But some filesystems sometimes getting read-only. So I need to check and write this. I can use while-do if filesystem read-only or not like this(simple touch tmp file):

#!/bin/bash

while read -r diskpath2;
do 
diskpath=${diskpath2}

touch $diskpath/readonly-check.tmp

if [ ! -f $diskpath/readonly-check.tmp ]; then
readonly="yes"
else
readonly="no"
fi

echo "$diskpath --> $readonly"

rm -rf $diskpath/readonly-check.tmp

done < disk.conf

I need to use that in my array for every filesystems and if read-only, write like this:

11/06/2015, /, 18%(READ-ONLY)
11/06/2015, /dev, 1%(READ-ONLY)
11/06/2015, /bin, 5%

disk.conf:

/
/dev
/bin

How can I do this?

NeronLeVelu
#!/bin/bash

DateSed="$( date +'%d/%m/%Y' )"

if [ -a disk.conf ]
  then
    cat disk.conf
  else
    # list of default disk (1 by line) to test
    echo "/"
  fi \
 | while read ThisDisk
    do
      # return status of this disk
      echo "${DateSed}, ${ThisDisk}, $( df -P "${ThisDisk}" | sed 's#.*[[:space:]]\([0-9]\{1,\}[%]\)[[:space:]]/.*#\1#;2!d' )$( [ ! -w "${ThisDisk}" ] && echo "(Read-Only)" )"
    done
  • change a bit the script treating each disk one by one in a loop
  • use several $() based on the Disk name for each info
  • assuming that there is no disk name/ mount point that contain AnyNumber%

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I add the variable every loop?

How can I use a for loop to create an array and assign linear values?

Javascript ' Array ' and ' Loop ' how can " variable[i] " be a CONDITION

How can I use Perl default array variable @_ with push?

How can I use the for loop's initialization variable as a placeholder in C?

How can I use an array index as form name in foreach loop?

Can I use a variable as index in for loop in python?

How can I use an array key in a powershell loop?

How can I use a loop variable in for cycle .bat

How can I loop this array?

How can I use a loop index to reference variable names and create new variable names?

how can i use for loop scope variable form outside for loop?

How can I use an array variable to IF statement?

Can I use variable in a .csv file in the for loop?

For loop, how can i use loop variable outside loop

how can i use the i variable outside the for loop?

To build a for loop, how can I use variable names in count() command?

R: How can I use loop variable i in the extract symbol $ of a dataframe?

How can i get specific values from array use for in loop

How can I use this for loop variable in django?

How can I use array_replace inside foreach loop?

How can i use a variable in a for loop in a command ? (c#)

How can I use a for loop to modify a variable inside a string while I download a list of images using BS?

How can I use a string variable in .bat for-loop?

how can i use multiple returned indices in a for loop to update a variable?

How can I use dynamic variable numbers in a loop?

How can I use jQuery loop variable to start at 2 while maintaining field removal option?

Can I use array variable to define typescript?

how can i use an inputed variable from user in a loop and outside the loop