0

i have to do this script

./myscript.sh /a/dir1/vol/vol0
4%

i need to create a script then run it while typing the directory of mounted volume in the same line. then it should show only the percent of usage

  • as you can see, there is the script and then I have to type a directory of mounted volume. then the output will be just percent what i did was df -h | grep vol0 | awk '{print $5}' 16% but the problem is I can't turn it to script then provide the directory of mounted volume. sorry i am really a beginner. – kukiaz Oct 16 '16 at 9:34
  • No worries. So, your problem is that you don't know how to pass a parameter from the command line to the script? – maulinglawns Oct 16 '16 at 9:43
  • Thank you for understanding, Yes, and actually I am really confused if it is really possible to execute the script like Line 1: ./myscript.sh /a/dir1/vol/vol0 Lilne 2: 4% line 1 will run myscript.sh and will use /a/dir1/vol/vol0 as input ( input can be changed depending on target volume) line 2 will just show the current use% – kukiaz Oct 16 '16 at 9:49
0

If I understand correctly, you want to pass an argument (the mounted volume) to your script. Here is a very simple example (dfScript.sh) of how I would do that:

#!/bin/bash

# Get the volume from command line
volume="$1"

df -h "$volume" | egrep -o '[0-9]+%'

exit 0

Calling this would look something like:

./dfScript.sh /home/
12%

$1 is the first argument passed to the script, in this case I used my /home directory as an example, but you can of course provide the path to any volume.

Also, you don't really need to assign $1 to a variable to use it, I just did so above for clarity.

You can read more on passing arguments to bash here.

| improve this answer | |
  • woah! exactly how it should be. I will remember this and play with it to familiarize it. Really thank you for your help! – kukiaz Oct 16 '16 at 10:07
  • No problem. Read the link I provided, it will tell you a lot about passing arguments to Bash and how it works (and doesn't work!). – maulinglawns Oct 16 '16 at 10:10
  • I will, thank you for the reference as well. It will really be helpful. – kukiaz Oct 16 '16 at 10:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.