0

I'm working on shell script and trying to split user input into multiple variable and use them at different places.

User input is not fixed so can't really assign fixed number of variable, input is separated by comma ,

./user_input.ksh -string /m01,/m02,/m03

#!/bin/ksh
STR=$2

function showMounts {
    echo "$STR"

    arr=($(tr ',' ' ' <<< "$STR"))
    printf "%s\n" "$(arr[@]}"

   for x in "$(arr[@]}"
     do
       free_space=`df -h "$x" | grep -v "Avail" | awk '{print $4}'`
       echo "$x": free_space "$free_space"
    done

#total_free_space = <total of $free_space>
#echo "$total_free_space"
}

Basically $STR* variable value is filesystem mount points

Host output if run separate df -h command

$ df -h /m01 | grep -v "Avail" | awk '{print $4}'

***Output***
 150

Current problems:

(working)1. How to get free space available for each /m* using df -h?
9
  • It is not clear how you want to split. Provide few more examples to clarify. Commented Sep 22, 2015 at 15:38
  • @anubhava I provided echo as example, thanks. Commented Sep 22, 2015 at 15:43
  • df -h /m01 | grep -v "Avail" | awk {'print $4'} --> give me 150, but this doesn't work in script Commented Sep 22, 2015 at 18:08
  • @EdMorton thanks, updated questions with what I used and what is the error and what I'm trying to resolve. Commented Sep 22, 2015 at 18:49
  • @EdMorton arr was typo, I fixed it. Error log points to line where I start free_space =.... My main goal is to 1. Check all mounts exist which entered by user, if not then exit 2. Get available free space at each mounts 3. Get total free space combining sum #2 Commented Sep 22, 2015 at 19:06

4 Answers 4

2

Easiest thing to do is to use shell array here like this:

#!/bin/ksh
str='/m01,/m02,/m03'

arr=($(tr ',' ' ' <<< "$str"))
printf "%s\n" "${arr[@]}"

Output:

/m01
/m02
/m03

To read elements individually you can use:

"${arr[0]}"
"${arr[1]}"
...

Update: Here is your corrected script:

#!/bin/ksh
STR="$2"

arr=($(tr ',' ' ' <<< "$STR"))
printf "<%s>\n" "${arr[@]}"

for x in "${arr[@]}"; do
   echo "$x"
   free_space=`df -h "$x" | awk '!/Avail/{print $4}'`
   echo "$free_space"
done
Sign up to request clarification or add additional context in comments.

10 Comments

great it displays all mounts, can you also please show me how can I use each of them to 1. check whether it present on server or not (ls -l ${arr[0]}), exit if not present and 2. Available free space. I get available free space directly from host like df -h /m01 | grep % awk {'print $4'} sed 's/%//g' 3. Total available free space from all array variable. Initially I thought I will know what all elements will be there but if it's generated dynamically then it will not be possible.
added two spaces for linebreak but somehow it didn't work.
corrected as recommended, receives couple of error 1. for /m01 error: free_space: not found [No such file or directory] 2. for /m02, /m03 error: /m02, /m03 No such file or directory (basically mount point doesn't exist for /m02, /m03 so trying to exit if such thing happens.
it was typo while writting here, edited that. I think there must be different way to use df -h within script.
try my updated answer, I tried to provide you a corrected script.
|
0

you can try,

#!/bin/ksh
STR=/m01,/m02,/m03
read STR1 STR2 STR3 <<<`echo $STR | awk 'BEGIN{FS=","; OFS=" "}{$1=$1; print}'`
echo $STR1 - $STR2 - $STR3

you get:

/m01 - /m02 - /m03

Comments

0

A variation on the theme:

# cat user_input.ksh

#!/bin/ksh
c=1
for i in $(echo ${@} | tr "," " ")
do
    eval STR$c="$i"
    ((c=c+1))
done

printf "\$STR1 = %s; \$STR2 = %s; \$STR3 = %s; ...\n" "$STR1" "$STR2" "$STR3"

Which gives you:

# ksh ./user_input.ksh /m01,/m02,/m03,/m04
$STR1 = /m01; $STR2 = /m02; $STR3 = /m03; ...

Hope that helps..

--ab1

Comments

0
$ cat tst.sh
str='/m01,/m02,/m03'
IFS=,
set -- $str
for i
do
    echo "$i"
done

$ ./tst.sh
/m01
/m02
/m03

Don't use all-upper-case for variable names unless you are going to export them (by convention and to avoid clashes with built in names like HOME, PATH, IFS, etc.).

For your overall script, you should simply be doing something like this:

df -h "${str//,/ }" | awk '/^ /{print $5, $3; sum+=$3} END{print sum}'

depending on what your df -h output looks like and what you're final output is supposed to be.

2 Comments

agree for upper case comment, will change it. thanks.
Thank you for all wise advice, it does teach me various aspect of scripting and SORRY if I sounded like person who is not following instructions. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.