Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
--------------------------------------------------------------
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"
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.
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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.