ssh error

Hi,

I'm executing the set of commands in remote machine via ssh command. But the control is not even going inside the if statement (not echo-ing anything).

/usr/bin/ssh  ${2}@${1} <<EOF >/var/tmp/housekeeping_${1}_${2}_`date +%Y%m%d%H%M%S`.log

if [ `df -h . |cut -d'%' -f1 |tail -1|awk '{print $NF}'` -eq 100 ]; then
echo -e "File system is 100% full, hence removing the below files..."
find . -name "*.log.gz" -mtime +10
#find . -name "*.log.gz" -mtime +10  -exec rm -f {} \;
echo -e "File system is 100% full, hence compressing the below files..."
find . -name "*.log" -mtime +7
#find . -name "*.log" -mtime +7  -exec gzip {} \;
if [ `df -h . |cut -d'%' -f1 |tail -1|awk '{print $NF}'` -eq 100 ]; then

echo -e "File system is 100% full, hence removing the below files..."
find . -name "*.log.gz" -mtime +5
#find . -name "*.log.gz" -mtime +5 -exec rm -f {} \;
echo -e "File system is 100% full, hence compressing the below files..."
find . -name "*.log" -mtime +3
#find . -name "*.log" -mtime +3  -exec gzip {} \;
fi
fi
EOF

i get the below message when i execute the script,

TERM environment variable not set.

please help me to overcome this problem

what if you use -t option in ssh

/usr/bin/ssh -t ${2}@${1} <<EOF >/var/tmp/housekeeping_${1}_${2}_`date +%Y%m%d%H%M%S`.log
...

No, even ssh with -t option couldn't resolve my problem. Experts please help me. :frowning:

Let try

usr/bin/ssh -T ${2}@${1} <<EOF >/var/tmp/housekeeping_${1}_${2}_`date +%Y%m%d%H%M%S`.log

Hey ygemici, excellent man its working for me. Thanks a ton:b: :slight_smile: :smiley:

:D;)

Hi

now another problem has come up.

inside the ssh iam using the following command

 df -h . |tail -1 |cut -d'%' -f1 |awk '{print $NF}' 

this should ideally give only the use%. however its displaying me the entire like like below,

                       48G   43G  5.3G  89

Please help.

How about ...

[house@leonov] df -h .
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda4             375G  140G  236G  38% /home
[house@leonov] df -h . | tail -1 | awk -F ' ' '{print $5}' | sed 's/\%//'
38

Hi.

My guess would be that the shell is trying to evaluate $NF - which evaluates to nothing, leaving just print (which prints the whole line).

Try:

 df -h . |tail -1 |cut -d'%' -f1 |awk '{print \$NF}'
df -h . |tail -1 |cut -d'%' -f1 |awk '{print $4}'

scottn is right

[root@localhost ~]# df -h . |tail -1 |cut -d'%' -f1 |awk '{print $NF}'
57

[root@localhost ~]# ssh localhost "df -h . |tail -1 |cut -d'%' -f1 |awk '{print $NF}'"
root@localhost's password: 
/dev/sda5              29G   16G   13G  57

[root@localhost ~]# ssh localhost "df -h . |tail -1 |cut -d'%' -f1 |awk '{print \$NF}'"
root@localhost's password: 
57

@ygemici
your solution wont work either, because of the unmasked $ in awk

[root@localhost ~]# ssh localhost << EOF
> df -h . |tail -1 |cut -d'%' -f1 |awk '{print $4}'
> EOF
Pseudo-terminal will not be allocated because stdin is not a terminal.
root@localhost's password: 
/dev/sda5              29G   16G   13G  57

Thanks experts, still i'm in problem.

Now

" df -h . |tail -1 |cut -d'%' -f1 |awk '{print \$NF}' "

gives the desired result, however if I use that code inside a if condition (listed below) it is not working properly.

if [ `df -h . |tail -1 |cut -d'%' -f1 |awk '{print \$NF}'` -ge 85 ]; then
echo -e "File system is  above 85% full, hence removing the below files..."
find . -name "*.log.gz" -mtime +10
#find . -name "*.log.gz" -mtime +10  -exec rm -f {} \;
echo -e "File system is 100% full, hence compressing the below files..."
find . -name "*.log" -mtime +7
#find . -name "*.log" -mtime +7  -exec gzip {} \;

As the above if statement didn't work,control is not coming inside the if condition. When i tried to use the below variable in if statement, it also fails. when i do echo of $fs_usage, it displays nothing.

fs_usage=`df -h . |tail -1 |cut -d'%' -f1 |awk '{print \$NF}'`

I'm really trired with this cript. Is there any other way to do some operation (cleanup operation) based on the filesystem usage in ssh?

Experts,

Please help me on this.

I would start by losing the backticks... they really are nothing but trouble (as a general rule STOP using them, PERIOD - anywhere - forever! :D)

Change:

if [ `df -h . |tail -1 |cut -d'%' -f1 |awk '{print \$NF}'` -ge 85 ]; then

to:

if [ \$(df -h . |tail -1 |cut -d'%' -f1 |awk '{print \$NF}') -ge 85 ]; then

Hi apsprabhu
Let the change this line.We see what happen :slight_smile:

if [ `df -h . |tail -1 |cut -d'%' -f1 |awk '{print \$NF}'` -ge 85 ]; then
if [ $( (df -h . | sed -e '/^Filesystem/d'  -e 's/.*G//' -e 's/% \///') ) -ge 25 ]; then

Thanks a lot SCOTTN, it works. after lot of hurdles i have completed the script .

Thanks to all of the folks in thie forum who has helped me in this task.