Problem assigning cmd output to variable then using in IF statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem assigning cmd output to variable then using in IF statement
# 1  
Old 09-14-2011
Problem assigning cmd output to variable then using in IF statement

Hi,

I'm using the bourn shell on a Sun Solaris Unix system. I am relatively new to UNIX scripting so please bear with me...

I'm having a couple issues:

1) I need to have a variable $FSIZE set with the output of a command each time the script runs. (the command looks for a file and gets its fize size in bytes).

2) I need to have the appended output text from du -b stripped off before assigning the file size to the $FSIZE variable in order to for #3 to work.

3) Then I need to use the $FSIZE in my first IF clause's expression to be part of the evaluation. If the file size is > 220 bytes then send an email and stop execution of the script at that point.

I am wrapping my code with the code tags so I hope it worked...If I was unclear about anything, please let me know. Thank you for the help.

Here is my code:
Code:
 
#!/bin/sh -x
#
EXISTINGFILE="/test/inbound/finaltickets.csv"
du -b $EXISTINGFILE | read size1
FSIZE=$size1
echo $FSIZE
if [ $FSIZE -lt 220 ]
then
echo "Error: The existing file is less than 220 bytes.  This indicates a possible corrupt file." | mailx -s "Existing file less than 220 bytes error" fname.lname@email.com
exit 0
#If this condition is true, then exist the script entirely after the email has been sent.
fi

if [[ -s $EXISTINGFILE ]];
then
head -1 $EXISTINGFILE | grep '^Ticket Number Number,Ack_Line Number,Ack_NSN,Ack_Fedstrip' >/dev/null 2>&1
if (( $? == 0 ));
then
#Yes-Header pattern was found and file was greater than 0 bytes.
echo "Warning: Process found an existing inbound file containing records." | mailx -s "Process Issue (File Already Exists) - Please Investigate" fname.lname@email.com
cat /test/inbound/newtickets.*.csv >> /test/inbound/finaltickets.csv
mv /test/inbound/newtickets.*.csv /test/inbound/archive/

else
#No-Header pattern was not found and file was greater than 0 bytes.
cat /test/inbound/newtickets.*.csv >> /test/inbound/finaltickets.csv
sed -e '1i\Ticket Number Number,Ack_Line Number,Ack_NSN,Ack_Fedstrip' /test/inbound/finaltickets.csv > /test/inbound/finaltickets.csv.tmp
mv /test/inbound/finaltickets.csv.tmp /test/inbound/finaltickets.csv
mv /test/inbound/newtickets.*.csv /test/inbound/archive/
exit 1
fi

else
#No-Header pattern was not found and filter is 0 bytes.
cat /test/inbound/newtickets.*.csv >> /test/inbound/finaltickets.csv
sed -e '1i\Ticket Number Number,Ack_Line Number,Ack_NSN,Ack_Fedstrip' /test/inbound/finaltickets.csv > /test/inbound/finaltickets.csv.tmp
chmod 775 /test/inbound/finaltickets.csv.tmp
mv /test/inbound/finaltickets.csv.tmp /test/inbound/finaltickets.csv
mv /test/inbound/newtickets.*.csv /test/inbound/archive/
exit 2
fi
exit 0

I will post a reply with the results I am getting trying to run this script shortly after this one.

Thanks,
D

---------- Post updated at 11:01 AM ---------- Previous update was at 10:58 AM ----------

As mentioned, here is the results I get attempting to run the script:

Code:
 
+ EXISTINGFILE=/test/inbound/finaltickets.csv
+ du -b /test/inbound/finaltickets.csv
+ read size1
+ FSIZE=
+ echo
+ '[' -lt 220 ']'
/test/inbound/./test_wip.sh: line 9: [: -lt: unary operator expected
+ [[ -s /test/inbound/finaltickets.csv ]]
+ head -1 /test/inbound/finaltickets.csv
+ grep '^Ticket Number,Ack_Line Number,Ack_NSN,Ack_Fedstrip'
+ ((  0 == 0  ))
+ echo 'Warning: Process found an existing inbound file containing records.'
+ mailx -s 'Process Issue (File Already Exists) - Please Investigate' fname.lname@email.com
+ cat /test/inbound/newtickets.20110825.073015.csv
+ mv /test/inbound/newtickets.20110825.073015.csv /test/inbound/archive/
+ exit 0

Please note that if my script did not have the following code in it, it executes successfully. So I am obviously not incorporating the "fize size" piece of the code in correctly...

Code:
 
du -b $EXISTINGFILE | read size1
FSIZE=$size1
echo $FSIZE
if [ $FSIZE -lt 220 ]
then
echo "Error: The existing file is less than 220 bytes.  This indicates a possible corrupt file." | mailx -s "Existing file less than 220 bytes error" fname.lname@email.com
exit 0
#If this condition is true, then exist the script entirely after the email has been sent.
fi

Thanks UNIX gurus,
D
# 2  
Old 09-14-2011
Code:
$ VAR=32
$ echo 33 | read VAR
$ echo $VAR
32
$

In any shell but a very modern ksh, 'read VAR' will run in a separate subshell because of the pipe: A new, independent shell which runs 'read VAR' in itself and not the parent program.

Try this instead:

Code:
size1=`du -b "$EXISTINGFILE"`

# 3  
Old 09-14-2011
Corona688,

Thanks! That got the variable to take. I think that takes care of #1.

Can you help shed light on #2 and #3? I'm going to see what I can try (again) to make it work...

This is the error I received while trying to run the updated code. I know it has to do with the value in the $FSIZE because du -b outputs the file bytes and the (path)/name of the file.

Code:
 
+ EXISTINGFILE=/test/inbound/finaltickets.csv
++ du -b /test/inbound/finaltickets.csv
+ size1='364    /test/inbound/finaltickets.csv'
+ FSIZE='364    /test/inbound/finaltickets.csv'
+ echo 364 /test/inbound/finaltickets.csv
364 /test/inbound/finaltickets.csv
+ '[' 364 /test/inbound/finaltickets.csv -lt 220 ']'
/test/inbound/./test_wip.sh: line 10: [: too many arguments
+ [[ -s /test/inbound/finaltickets.csv ]]

---------- Post updated at 11:56 AM ---------- Previous update was at 11:46 AM ----------

I guess my challenge with #2 is that I want to always extract only the bytes info (numbers) from the du -b [file] output, but that value can vary in length. Substringing only the byte data is important since I'll be using $FSIZE in the first IF clause...
# 4  
Old 09-14-2011
Ah, it prints two columns, the value and the name...

You can split that with set --, which sets $1 $2 ... into the value you give it.

Code:
set -- `du -b "$EXISTINGFILE"`
VAR=$1

# 5  
Old 09-14-2011
Error

Hi Corona688,

So, I tried to incorporate your set-- suggestion. I obviously couldn't do it correctly after several attempts. If you wouldn't mind, please explain to me using my code how I should have added it?

So, this was my alternative and it seems to work:
Code:
 
ls -la /test/inbound/finaltickets.csv | awk '{print $9, "\t", $5}'

I'm seeing the light at the end of the tunnel now Smilie. But, the issue now is this error I'm getting executing the script (the file size = 0 bytes in this test):

Code:
 
+ EXISTINGFILE=/test/inbound/finaltickets.csv
++ ls -la /test/inbound/finaltickets.csv
++ awk '{print $9, "\t", $5}'
+ size1='        0'
+ FSIZE='        0'
+ echo 0
0
+ '[' 0 -lt 220
/test/inbound/./test_wip.sh: line 14: [: missing `]'
+ [[ -s /test/inbound/finaltickets.csv ]]
+ cat /test/inbound/newtickets.20110825.073015.csv.......

The line 14: error is referring to this section of my code below, specifically where the IF expression is being evaluated.
Does it not like the && $FSIZE != 0? Smilie

Code:
 
#!/bin/sh -x
#
ALERTEMAIL=`echo "Process Issue (File Already Exists) - Please Investigate" fname.lname@email.com." | mailx -s "Process Issue (File Already Exists) - Please Investigate" fname.lname@email.com`
EXISTINGFILE="/test/inbound/finaltickets.csv"
size1=`ls -la "$EXISTINGFILE" | awk '{print $9, "\t", $5}'`
FSIZE=$size1
echo $FSIZE
if [ $FSIZE -lt 220 && $FSIZE != 0 ]
then
echo "Error: The existing file is less than 220 bytes.  This indicates a possible corrupt file." | mailx -s "Existing file less than 220 bytes error" fname.lname@email.gov
exit 0
fi

Thanks for all your help so far!

D

Last edited by dqrgk0; 09-14-2011 at 04:38 PM.. Reason: masked work related info
# 6  
Old 09-14-2011
Quote:
Originally Posted by dqrgk0
Hi Corona688,

So, I tried to incorporate your set-- suggestion. I obviously couldn't do it correctly after several attempts.
The way I posted worked. I can't see your computer from here, how about you tell me what you tried? Smilie

Your awk script doesn't work quite right because your awk is printing three fields when you only wanted one, which really defeats the point. Using awk to operate on a single line instead of using a shell builtin is literally hundreds of times slower, too.
# 7  
Old 09-14-2011
Okay fair enough! If I knew ahead of time, I would have saved my attempts Smilie. I'll relive my misery to get the right code I suppose.

I appreciate the advice about the awk. In that case, I would prefer to understand and get the set -- to work.

I'm at home now, but will get back to you tomorrow morning.

Regards,
D
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning bc output to a variable

I'm converting decimal to integer with bc, and I'd like to assign the integer output from bc to a variable 'val'. E.g. In the code below: If b is 5000.000, lines 6 and 8 will output: 5000 (5000.000+0.5)/1 | bc I'd like val to take the value 5000 though, rather than 5000.000 Does someone... (3 Replies)
Discussion started by: pina
3 Replies

2. Shell Programming and Scripting

Assigning output from awk to variable

I have a script whose contents are as below result= awk 's=100 END {print s }' echo "The result is" $result The desired output is The result is 100 My script is running without exiting and i am also not getting the desired output. Please help (5 Replies)
Discussion started by: bk_12345
5 Replies

3. Shell Programming and Scripting

Assigning Variable to AWK statement

Hi, The following command runs on in the Korn shell prompt. however i want to output the value of this to a variable. Can anyone provide a solution? echo 'ABC,DEF,"G,HI,J",KLM,"MNi,O"'| awk -F "\"" '{for(i=1;i<=NF;i++){if(i%2)gsub("\,","~^~",$i)}}1' (2 Replies)
Discussion started by: ladarlsan
2 Replies

4. Shell Programming and Scripting

Piping and assigning output to a variable in Perl

Hi All, I am trying to convert the below Csh code into Perl. But i have the following error. Can any expert help ? Error: ls: *tac: No such file or directory Csh set $ST_file = `ls -rt *$testid*st*|tail -1`; Perl my $ST_file = `ls -rt *$testid*st*|tail -1`; (10 Replies)
Discussion started by: Raynon
10 Replies

5. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

6. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies

7. Shell Programming and Scripting

Assigning output of a command to variable

When I run time -p <command>, it outputs: real X.XX user X.XX sys X.XXwhere X.XX is seconds. How I can take just that first number output, the seconds of real time, and assign that to a variable? (9 Replies)
Discussion started by: jeriryan87
9 Replies

8. Shell Programming and Scripting

Assigning output to a variable

I am new to unix shell scripting. I was trying to convert each lines in a file to upper case. I know how to convert the whole file. But here i have to do line by line. I am getting it in the below mentioned script #!/bin/bash #converting lower to upper in a file #tr "" "" <file1... (3 Replies)
Discussion started by: jpmena
3 Replies

9. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

10. Shell Programming and Scripting

Assigning output of command to a variable

Hi, I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string. Contents of test.txt --> This is a test var1="`head -n 1 test.txt`" echo $var1 (This is a... (5 Replies)
Discussion started by: oma04
5 Replies
Login or Register to Ask a Question