LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-04-2008, 05:28 AM   #1
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Rep: Reputation: 15
scripting error


Hi
new bie for scripting in linux
i want to write a script which takes the string as the argument and remove the files by going into that specific folder.
the script looks like this:

set JAN FEB MAR APR
echo $1 $2 $3 $4
i=1
while [$i -le 4]
do
find /home/susee/test/$i/*/data/ -type f -name "*.247" -exec rm {} \;
i='expr $i + 1'
done

but i'm getting the following error

test.sh: line 5: [1: command not found

where am i going wrong?
 
Old 09-04-2008, 05:45 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You have to put a space between the square brackets and the expression inside:
Code:
while [ $i -le 4 ]
Moreover, use the code tags to embed lines of code. In this way the formatting is preserved. To put code tags either go in advanced mode, select the lines of code and press the # button, or explicitly write [CODE] and [/CODE] before and after the lines respectively.
 
Old 09-04-2008, 06:00 AM   #3
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Now the error is like this

--------------------------------------------------------------
find: /home/susee/test/1/*/data/: No such file or directory
test.sh: line 5: [: expr $i + 1: integer expression expected
--------------------------------------------------------------
why it is taking '1' as argument instead of 'AU'?

and i didnt get what is "integer expression expected"

Last edited by nanda22; 09-04-2008 at 06:01 AM.
 
Old 09-04-2008, 06:05 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
What is 'AU'? Please, post the last version of your script, using code tags.

The error should be
Code:
i='expr $i + 1'
here you have to use back ticks (not single quotes) to do command substitution. Or better the $(command) syntax which has some advantages over back ticks:
Code:
i=`expr $i + 1`    # or the equivalent
i=$(expr $i + 1)
You may also consider the arithmetic expansion with the double parentheses construct, in bash.
 
Old 09-05-2008, 03:13 AM   #5
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Code:
IFS=:
set A B C D
echo $1 $2 $3 $4
i=1
while [ $i -le 3 ]
do
find /home/susee/test/$i/*/data/ -type f -name "*.247"  -exec rm {} \;
i=$(expr $i + 1)
done
Now the error is
-----------------------------------------------------------
find: /home/susee/test/1/*/data/: No such file or directory
find: /home/susee/test/2/*/data/: No such file or directory
find: /home/susee/test/3/*/data/: No such file or directory
------------------------------------------------------------

The A,B,C and D are folders, which contain the data.
 
Old 09-05-2008, 04:34 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You have to carefully check the path: are you sure you have some directories under 1, 2 and 3...? Moreover, for testing purposes better put an echo in front of the rm command, otherwise you may end up with unwanted deleting of some files:
Code:
find /home/susee/test/$i/*/data/ -type f -name "*.247"  -exec echo rm {} \;
or you can use the -ok action instead of -exec. This will ask you confirmation before actually executing the rm command:
Code:
find /home/susee/test/$i/*/data/ -type f -name "*.247" -ok rm {} \;
 
Old 09-05-2008, 04:34 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Rather than using set to replace the positional parameters, you might consider this simpler and clearer by to iterative over the list of months:

Code:
#!/bin/bash

for month in JAN FEB MAR APR; do
    find /home/susee/test/$month/*/data/ -type f -name "*.247" -exec rm {} \;
done
Note that file names on unix-like systems (such as Linux) are case sensitive. If you are getting "file not found" messages, check the path is correct - including the case of all characters.

If there are a large number of files to delete, the method above can be somewhat inefficient, as an rm process is invoked for every file. There is a more efficient method using xargs, although it is a little harder to understand how it works. If you are interested in this other method, please ask. I don't want to post it right away to avoid overwhelming you. :-)

Last edited by matthewg42; 09-05-2008 at 04:36 AM.
 
Old 09-05-2008, 06:51 AM   #8
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Dear matthewg42,

I would be obviously grateful to you, if you are going to teach me something more.
 
Old 09-05-2008, 07:00 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
OK, it's quite a long explanation, but it should give you some idea how it works:

http://www.linuxquestions.org/questi...1/#post2985881
 
  


Reply

Tags
err


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell scripting error shouup Programming 8 09-08-2005 12:38 PM
BASH scripting error Darklight451 Linux - General 4 11-22-2004 03:59 PM
Shell scripting error bindweb Programming 4 09-30-2004 10:50 AM
Newbie : expr scripting error hrg Linux - General 3 08-13-2004 10:24 AM
Error stop when scripting eXor Linux - Software 10 06-12-2003 02:06 PM


All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration