if statement code syntax


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers if statement code syntax
# 1  
Old 01-07-2010
if statement code syntax

Hi, can someone please tell me what is wrong with this code? I just want it to check if the file size is greater than 2000kb.

Code:
if [ du -sk $FILE -gt 2000 ]

Thanks!

---------- Post updated at 09:23 PM ---------- Previous update was at 09:21 PM ----------

I should probably post the full code:

Code:
#!/bin/sh
#Daniel Heidt's program to sort archive shots into folders by document markers

filecount=1
mkdir $filecount

for FILE in DSC*
do
     if [ $FILE != "test.sh" ]
	then
		if [ du -sk $FILE -gt 2000 ] #dumps file into existing $filecount folder - will later be used if image is not marker
		  then
		  echo $FILE
		  mv "$FILE" "$filecount"
		else #bumps filecount - will later be used to skip over marker shots
		  let filecount=filecount+1
		  mkdir $filecount
		fi
     fi
done

# 2  
Old 01-07-2010
Try using back ticks around your command that you want to execute:

if [ `du -sk $FILE` -gt 2000 ]
# 3  
Old 01-07-2010
Good suggestion - but I'm still getting the same error: "[: too many arguments"

Other suggestions? Am I trying to do too much in one if statement? Should I perhaps read the result of "du -sk $FILE" into a variable and then compare it within the if statement?
# 4  
Old 01-07-2010
Try running the du command by itself from the command line. I'm thinking it might be outputting something more than just an interger. If so then that's why it's unable to compare it with another interger and so the test fails.

If the output is more spitting out more than just a number, then you might want to pipe it through awk to get the field you need. I would suggest assigning the output to a variable and then using that in your test statement. Let me know what happens.
# 5  
Old 01-08-2010
Try this

Code:
if [ `du -sk $FILE` -gt 2000 ];then

# 6  
Old 01-10-2010
Hi you're right, I'm not getting a simple integer readout. I'm getting this:

Code:
$ du dsc03143.jpg
3784	dsc03143.jpg

I am extremely unfamiliar with the awk command and haven't been able to find a tutorial that makes much sense to me yet. Can anyone help me out with isolating the integer so that it can be compared? I know its a simple string isolation but I can't figure out how to do it. Or is there an easier way to only get the number?
# 7  
Old 01-10-2010
Code:
$ du dsc03143.jpg | awk '{print $1}'

# so...
if [ $(du dsc03143.jpg | awk '{print $1}') -gt 2000 ]; then


Last edited by Scott; 01-10-2010 at 07:49 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Syntax for if statement

I'm new to unix and the command line and am trying to learn different commands. I have a file (teledir.txt) that contains a name and phone number for 3 different people. I am writing a script that is to take two positional parameters and I typed out how it should behave: if <name and number... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

Help with if statement syntax in shell script

I want to make the file test condition a variable ($Prmshn in code below). My goal is to use something like the first three unsuccessful if statetments since the 'if #!/bin/ksh test_input() { Prmshn=${1} InFLNm=${2} ifReq="-$Prmshn $InFLNm" #the following three if statments fail: #if ] ;... (10 Replies)
Discussion started by: ms63707
10 Replies

3. Shell Programming and Scripting

[Solved] 0403-057 Syntax error for if statement

I am getting the following error when I am running a script in ksh when trying to execute an if statement comparing two numerical values tstmb.sh: 1.5321e+08: 0403-057 Syntax error Below is my code snippet. #!/bin/ksh set -x TODAY=$(date +%y%m%d) for file in $(ls -rt *.log | tail... (11 Replies)
Discussion started by: kiran1112
11 Replies

4. Shell Programming and Scripting

If statement Syntax error

Hi Can you please tell me what is wrong with this line: if && ]; then basically i want to check if x = 12 and F (Filename) end with 'g'. But it is throwing syntax error. (7 Replies)
Discussion started by: rtagarra
7 Replies

5. Shell Programming and Scripting

Help with if statement syntax

Hi, Is there a way to compare the value in if condition with a list of values. eg . if ] then echo "it's a mammal" else echo "its not" fi Thanks! (8 Replies)
Discussion started by: neil.k
8 Replies

6. Shell Programming and Scripting

complex if statement syntax without using 'if ..' keyword in ksh.

It saves me lot of typing and space/lines when I do not use full 'if' keyword and construct, instead use .. && <statement> || <statement> that perfectly replaces.. if ; then <statement> else <statement> fi Can I use following syntax when I want to add multiple statements under 'if'... (4 Replies)
Discussion started by: kchinnam
4 Replies

7. Shell Programming and Scripting

Syntax error in script with if statement

I'm working on a function in a shell script I'm writing that will eventually take in and print out a list of vendor names and aliases (for my work) Here's the function in question: addvendorandalias () { echo echo -n 'Would you like to create a new vendor list (y or n)? ' read answer... (3 Replies)
Discussion started by: Straitsfan
3 Replies

8. Shell Programming and Scripting

syntax error on if statement

Hi, Can you please help me with this one: I write an "if" statement, something like this: if then echo "big file" else echo "normal file" and I get an error: `'then is not expected Thanks in advance (6 Replies)
Discussion started by: apenkov
6 Replies

9. Shell Programming and Scripting

Confirming Syntax - IF statement.

Hi All, Has been a while since I was last on, so I hope everyone has been doing fine. ;) Would like to know if the below IF statement syntax is correct for a ksh environment. It's been pushed into live as someone had deleted the development copy(!); not withstanding that, the statement now... (3 Replies)
Discussion started by: Cameron
3 Replies

10. Shell Programming and Scripting

Need explanation for the syntax(code)

Hi I am new to shell script programming... want to know the process of the following: if then echo "$0: missing argument for option(s) :$MISSINGOPTARG" echo "usage" $USAGE" exit 1 fi (1 Reply)
Discussion started by: chandhar
1 Replies
Login or Register to Ask a Question