Trying to test space in filesystem on Unix - shell

I need to check if I Filesystem exists, and if it does exist there is 300 MB of space in it.
What I have so far:
if [ "$(df -m /opt/IBM | grep -vE '^Filesystem' | awk '{print ($3)}')" < "300" ]
then
echo "not enough space in the target filesystem"
exit 1
fi
This throws an error. I don't really know what I'm doing in shell.
My highest priority is AIX but I'm trying to get it to work for HP and Sun too.
Please help.
-Alex

Here is the code I got working.
if [ "$(df -m /opt/IBM/ITM | awk 'NR==2{print ($3)}')" -lt "300" ]
then
echo "not enough space in the target filesystem"
exit 1
fi

How about posting the error? Anyway, try the following syntax, ie. double brackets and no double quotes:
if [[ $(...) < 300 ]]; then
...
fi
From man bash:
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional
expression expression.

Related

How to get Memory Usage for a PID in a variable using bash

This is what I've been working with but haven't been successful,
if [ "$memUsage" -gt "30500" ];
then
transUsage=$(pmap 3097 | tail -n 1 | awk '/[0-9]/{print $2}')
transUsage=$transUsage | awk '/[0-9]/{print $2}' #This was my attempt at removing the extra K
if [ "$transUsage" -gt "10500" ];
then
echo "Terminated this and this"
fi
# Print the usage
echo "Memory Usage: $memUsage KB"
fi
I need memory usage of PID 3097 in variable so that I could use if command.
Currently it outputs,
xxxxK, where x is memory usage size. Due to K being part of size, it's not being being recognized as numeric value.
How to solve this? Would appreciate the help.
Regards!
You can use this better code:
#!/bin/bash
pid=$1
transUsage=$(pmap $pid | awk 'END{sub(/K/, "", $2); print $2}')
if ((transUsage > 10500)); then
echo "Terminated this and this"
fi
echo "Memory Usage: $transUsage KB"
((...)) is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed. See http://mywiki.wooledge.org/ArithmeticExpression.

Bash single-line nested for-loop is taking comparison variable as a command

I believe my error is in do if ($i -gt 100) area but I havent been able to figure it out.
My input is:
for i in `ps | cut -d ' ' -f1`; do if ($i -gt 100); then echo $i; fi; done
My output is this where the process IDs have been taken as commands.
bash: 13968: command not found
bash: 21732: command not found
bash: 21733: command not found
bash: 21734: command not found
How can I fix this and what is the relevant man page that I should read up on? Thank you.
if ($i -gt 100)
should be changed to
if [ $i -gt 100 ]
Note that there is a space before and after [], this is neccessary, otherwise you will get a syntax error (its because [ is a link to test in /usr/bin).
The relevant manapge would be man test, as [ is test.
Also, but this has nothing to do with the question, I recommend switchting from
`command`
to
$(command)
in bash.
It's not clear what your script is trying to do (the posted answers produce no output on my system) but if you want to print all PIDs that are greater than 100, here's how you'd do that:
$ ps | awk '$1 > 100{print $1}'
PID
314024
217880
230804
217084
263048
260788
218016
313464
201556
200732
just add spaces after and before brackets and the expression
for i in `ps | cut -d ' ' -f1`; do if [ $i -gt 100 ]; then echo $i; fi; done

Getting the line count of a file in a shell script with wc failing

my script check if the arguments are files or folders
if it is a file, he count the number of lines
after that, if the number of lines is great then 20 or less he do some instructions
the problem is in this instructionn= cat $a | wc -l
My script:
#!/usr/bin/env bash
echo 'Hello this is the test of' `date`
echo 'arguments number is ' $#
if [ $# -eq 4 ]
then
for a in $#
do
if [ -d $a ]
then
ls $a > /tmp/contenu
echo "contenu modified"
elif [ -f $a ]
then
# this instruction must set a numeric value into n
echo "my bad instruction"
n= cat $a | wc -l
echo "number of lines = " $n
# using the numeric value in a test (n must be numeric and takes the number of lines in the current file)
if [ $n -eq 0 ]
then
echo "empty file"
elif [ $n -gt 20 ]
then
echo ` head -n 10 $a `
else
cat $a
fi
else
echo "no file or directory found"
fi
done
else
echo "args number must be 4"
fi
This is the output of the execution of the incorrect instruction
my bad instruction
5
number of lines =
ExamenEx2.sh: line 19: [: -eq : opérateur unaire attendu
The line n= cat $a | wc -l is an offending instruction. Always remember that bash shell scripting is extremely case-sensitive. Your command is interpreted by the shell as having to run two separate commands
n= cat $a | wc -l
#^^ ^^^^^^^^^^^^^^
#1 2
The first part just stores an empty string to the variable n and the next prints the line count of the file stored in variable a. Notice that the shell does not throw errors for this. Because it is not violating the syntax (just the semantics are wrong). But the line count is never assigned to the variable n.
The error is seen when the conditional if [ $n -eq 0 ] is hit when you are doing a comparison with an empty variable on the LHS.
You wanted to run a command and store its output, you need command-substitution($(..)) for that. Assuming the $a contains a name of a file just do
n=$(wc -l < "$a")
Note, that I've removed the useless cat usage and piping it to wc. But wc can read from an input stream directly.
Also note that you have multiple bad practices in your script. Remember to do the following
Always double-quote the shell variables - "$#", "$#", [ -f "$a" ], [ -d "$a" ]
Don't use the `` for command-substitution, because it is not easily nestable and you might have issues related to quoting also.
You can use conditional expression [[ if you are sure if the script is running under bash in which a variable containing spaces can be used without quoting on the LHS

Compare length of sound file in bash

I want to have a shell script, that checks the length of a sound file and check if the length is shorter, than a specified length. But I keep getting an error message "command not found" at the if-statement.
#!/bin/bash
soundlength=$(soxi -D $1)
enter code here
if [$soundlength < $2]
then
# do something
fi
I am guessing the $soundlength is a string and it's failing to compare string to int, but I can't find a solution to this.
Thanks in advance for all answers.
The problem is that soxi returns 0 or a float - example 27.741995, therefore you will need bc or awk to check if the output is bigger than N, this because bash don't support floats.
Here is an example with bc:
#!/bin/bash
soundlength=$(soxi -D $1)
if [ 1 -eq "$(echo "${soundlength} > ${2}" | bc)" ]; then
echo "${soundlength} is > than ${2}"
fi
And here is an example with AWK:
#!/bin/bash
soundlength=$(soxi -D $1)
if awk 'BEGIN{exit ARGV[1]>ARGV[2]}' "$z" "$y"; then
echo "${soundlength} is > than ${2}"
fi
If you don't want to use either bc of awk you could give a try to zsh shell, it supports floats.

Bash IF statement

rather noobie question here.. i think. but i cant get this script to work. It is going to be included inside a script that i asked about here a few days ago (BASH output column formatting). basically i want to be able to scrape a site for a portion of text and return a ONLINE/OFFLINE answer. I apologize for poor formatting and weird variable names. Thanks for taking a look and helping me out!
#!/bin/bash
printf "" > /Users/USER12/Desktop/domainQueryString_output.txt
domainCurlRequest="curl https://www.google.com/?gws_rd=ssl"
ifStatementConditional="grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l"
echo $($domainCurlRequest) >> /Users/USER12/Desktop/domainQueryString_output.txt
if [ $ifStatementConditional -eq 2 ] ;
then second_check="online"
else second_check="DOMAIN IS OFFLINE"
fi
echo $second_check
i keep getting the following error when trying to run this script
/Users/USER12/Desktop/domain_status8working.sh: line 6: [: too many arguments
i tried to rewrite another way but got same errors so my logic or syntax or something is off.
Thanks again for taking a look and helping me out!!!
ifStatementConditional="grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l"
This is a string assignment. You probably want backticks, or the $() construct. Otherwise, $ifStatementConditional will never equal 2
if [ $ifStatementConditional -eq 2 ] ;
This is expanded as:
if [ grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l -eq 2 ] ;
Which explains your error.
I think you meant that:
#!/bin/bash
curl "https://www.google.com/?gws_rd=ssl" > /Users/USER12/Desktop/domainQueryString_output.txt
ifStatementConditional=$("grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l")
if [ $ifStatementConditional -eq 2 ] ; then
second_check="online"
else
second_check="DOMAIN IS OFFLINE"
fi
echo $second_check
No need to do printf "" > somefile.txt when you do a curl after, and you append to that file
$() is to capture subshell output. That what was missing here.

Resources