Unix if statement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unix if statement
# 1  
Old 03-30-2011
Unix if statement

Hi, this is my first post. Although I have already read through the Forum Rules, please pardon me if I violate anything.

I am trying to perform an if statement to check if a SET of files exist, and if they do, I would like to move them else where:

Code:
$DIRECTORY_1
if ls filename*.txt
then
  mv filename*.txt $DIRECTORY_2
else
  echo "File Not Found"
fi

It keeps printing "File Not Found" even though there are files in $DIRECTORY_1

Is there something wrong with my 'if ls...' statement? This command used to be working about a month ago.

Please help. Thank you!!

Last edited by Franklin52; 03-30-2011 at 10:50 AM.. Reason: Please use code tags and indentation
# 2  
Old 03-30-2011
With a loop like this you should get only existing files:
Code:
for file in filename*.txt
do
  mv .....
done

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 03-30-2011
Thanks Franklin,

If that's the case, with my limited ability, may I know how do I include the "File Not Found" part?

Also, out of curiosity, is there any reason that this worked before, and now it didn't? Thank you!!

---------- Post updated at 09:54 AM ---------- Previous update was at 09:53 AM ----------

Quote:
Originally Posted by Franklin52
With a loop like this you should get only existing files:
Code:
for file in filename*.txt
do
  mv .....
done

# 4  
Old 03-30-2011
I don't think your first code ever cd'ed into $DIRECTORY_1.
# 5  
Old 03-30-2011
Quote:
Originally Posted by Corona688
I don't think your first code ever cd'ed into $DIRECTORY_1.
Thanks for your input, Corona688. However when I tried to remove the if statement, it worked. This shows that it did cd'ed into $DIRECTORY_1. Let me know if I am not thinking right. Here's the code:

cd $DIRECTORY_1
mv filename*.txt $DIRECTORY_2

Thanks!
# 6  
Old 03-30-2011
Code:
cd $DIRECTORY_1

The highlighted portion was missing from your first try. Otherwise it should have worked, as far as I can tell.

Franklin's version is nearly equivalent though and avoids the useless use of ls. I'd make one addition to avoid errors when nothing matches:

Code:
for file in filename*.txt
do
        # If the file/dir doesn't exist, break the loop
        [ -e "$filename" ] || break
        mv "$filename" $DIRECTORY_2
done

When there's no matches, filename will be the literal string "filename*.txt" and running mv on that will complain no such file or directory..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*). #!/bin/sh #Get a string from user and check whether its a existing filename or not rm str2 rm str3 echo "enter a file \c" read fil... (8 Replies)
Discussion started by: Mohan0509
8 Replies

3. Shell Programming and Scripting

Why statement works in LINUX and not UNIX?

Hello, I have a ksh script that uses code below. For some reason it works under linux but fails in unix. Any idea why? if ]; then ... Thanks (9 Replies)
Discussion started by: rdogadin
9 Replies

4. Shell Programming and Scripting

If statement UNIX

Hi, I have an if-else statement here but I am receiving some error. if ; then ls -lrt /dev/myfolder/someword* | grep "Oct 02" | awk '{print $9}' else echo "some message" fi It does go to the else and execute the command there but I am still receiving this... (4 Replies)
Discussion started by: erin00
4 Replies

5. Shell Programming and Scripting

UNIX variable to SQL statement

The following is my script : #!/bin/bash echo "please give app_instance_id" read app_instance_id echo "id is $app_instance_id" export app_id=app_instance_id sqlplus -s nnviewer/lookup@//nasolora008.enterprisenet.org:1521/LOAD3 @test.sql<<EOF SPOOL /home/tibco/MCH/Data/qa/raak/name.xls... (4 Replies)
Discussion started by: raakeshr
4 Replies

6. UNIX for Dummies Questions & Answers

SQL statement is not work on unix script

Hi, I have the following basic script. However, the statement (line 5) is not work. The output data is not able to set my request format a30. Any advise? :mad: echo " Column filename format a30"|sqlplus4 echo Input file list to check: read filelist for file in `cat $filelist.txt` do... (1 Reply)
Discussion started by: happyv
1 Replies

7. UNIX for Dummies Questions & Answers

unix script with SQL statement problem

Hello, I have a script to get the information from database, however, it's look like the loop is not work, can someone help? :confused: echo Input file list to check: read filelist for file in 'cat $filelist.txt' do echo "select FILENAME from FILE_TABLE where filename like '${file}'%;" >>... (9 Replies)
Discussion started by: happyv
9 Replies

8. UNIX for Dummies Questions & Answers

case statement in UNIX scripting (ksh)

Hi, I have a script like below : #!/bin/ksh echo "Do you want to export all docs ?" read alld echo "Do you want to export template or report only " read temr case && ] #arguments ;; case && ] #arguments ;; case && ] #arguments ;; (4 Replies)
Discussion started by: luna_soleil
4 Replies

9. UNIX for Dummies Questions & Answers

Perl / Interpret Unix If statement

Hi all, I am a newbie, so please let me know if there is a better way to do this. I coded a perl script, that scans a Unix script and picks out certain strings (strings that precede ".log"). However I want to limit the strings it picks up based on the "If statements" in the unix script. ... (0 Replies)
Discussion started by: syera123
0 Replies

10. Shell Programming and Scripting

How to convert unix command into Awk statement

Hi all, How can i use the below unix command in AWK . Can any one please suggest me how i can use. sed -e "s/which first.sh/which \$0/g" $shell > $shell.sal where $0=current program name(say current.sh) $shell=second.sh (1 Reply)
Discussion started by: krishna_gnv
1 Replies
Login or Register to Ask a Question