[Solved] Error While Using "Basename" in the Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Error While Using "Basename" in the Script
# 1  
Old 03-21-2013
[Solved] Error While Using "Basename" in the Script

Hello All


My Requirement is to Delete files in a particular directory based on its extension. So for that i have piece of Code down below

Code:
horo=`date +%Y%m%d`
logfile=/temp/log.file.$horo
date >> $logfile

for fulldir in 'ls -ld /temp/strs/*'
do
dir=`basename $fulldir`
case $dir in
        *he|*out|*bdy)
        echo $fulldir eligible to cleaning >> $logfile
        find $fulldir -type f -mtime +1 ! \( -name '.profile'-o -name '*history
' \) -ls -exec rm -f {} \; >> $logfile
        ;;
        *)
        echo $fulldir excluded from cleaning >> $logfile
        ;;
esac
done


So from Above you can see that i need to delete files with extension *.he,*.out and *.bdy from the directory /temp/strs. And i need to exclude everything else.

But while running the Script i am facing an Error

Usage: basename string [suffix]


Any Clue?

Regards
Ajesh
# 2  
Old 03-21-2013
how about something more simple:

Code:
find /temp/strs -type f \( -iname "*.he" -or -iname "*.out" -or -iname "*.bdy" \) -exec rm -v {} \;

Padow
# 3  
Old 03-21-2013
Quote:
Originally Posted by Ajesh
Hello All


My Requirement is to Delete files in a particular directory based on its extension. So for that i have piece of Code down below

Code:
horo=`date +%Y%m%d`
logfile=/temp/log.file.$horo
date >> $logfile

for fulldir in 'ls -ld /temp/strs/*'
do
dir=`basename $fulldir`
case $dir in
        *he|*out|*bdy)
        echo $fulldir eligible to cleaning >> $logfile
        find $fulldir -type f -mtime +1 ! \( -name '.profile'-o -name '*history
' \) -ls -exec rm -f {} \; >> $logfile
        ;;
        *)
        echo $fulldir excluded from cleaning >> $logfile
        ;;
esac
done


So from Above you can see that i need to delete files with extension *.he,*.out and *.bdy from the directory /temp/strs. And i need to exclude everything else.

But while running the Script i am facing an Error

Usage: basename string [suffix]


Any Clue?

Regards
Ajesh

Since you have single quotes around the ls command, the for-loop only executes once and the value assigned to $fulldir is literally 'ls -ld /temp/strs/*'. Since the variable is unquoted, it will undergo field splitting. basename is then called with more arguments than it understands: (1) 'ls', (2) '-ld', (3...n) and all the filenames that matched /temp/strs/*.

If the ls command ran as intended, $fulldir would still give you problems because you're using a long listing (ls -l), which will insert into $fulldir dates, user ids, group ids, permission strings, dates, times, etc.

You do not need ls for this. It is an inferior solution to the shell's pattern expansion mechanism. In my code below, note that the trailing backslash of /temp/strs/*/ ensures that only directories are listed, and the line with the continue handles the special case of /temp/strs/*/ not matching anything and so expanding to itself, a non-existent directory.

Further, why are you even using basename at all? You don't seem to use "$dir" for anything but the case statement's pattern match. Since you're matching against the last component of the directory's name, the case pattern match will give identical results whether you're using the full pathname or the basename.

Code:
for fulldir in /temp/strs/*/
do
[ -d "$fulldir ] || continue
case $fulldir in
    *he|*out|*bdy)

You may want to give your logging a look over as well. You log the removal of a file before attempting the removal, but if rm fails, your log will not contain a record of that failure. You could redirect the find command's standard error to the same logfile so that rm will inherit it? Or, if not, place the -exec before the -ls, so that if the rm fails the file won't be listed as removed in the log. You can then log the find/rm error using the script's stderr. Just a thought.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 03-25-2013
Quote:
Originally Posted by alister
Since you have single quotes around the ls command, the for-loop only executes once and the value assigned to $fulldir is literally 'ls -ld /temp/strs/*'. Since the variable is unquoted, it will undergo field splitting. basename is then called with more arguments than it understands: (1) 'ls', (2) '-ld', (3...n) and all the filenames that matched /temp/strs/*.

If the ls command ran as intended, $fulldir would still give you problems because you're using a long listing (ls -l), which will insert into $fulldir dates, user ids, group ids, permission strings, dates, times, etc.

You do not need ls for this. It is an inferior solution to the shell's pattern expansion mechanism. In my code below, note that the trailing backslash of /temp/strs/*/ ensures that only directories are listed, and the line with the continue handles the special case of /temp/strs/*/ not matching anything and so expanding to itself, a non-existent directory.

Further, why are you even using basename at all? You don't seem to use "$dir" for anything but the case statement's pattern match. Since you're matching against the last component of the directory's name, the case pattern match will give identical results whether you're using the full pathname or the basename.

Code:
for fulldir in /temp/strs/*/
do
[ -d "$fulldir ] || continue
case $fulldir in
    *he|*out|*bdy)

You may want to give your logging a look over as well. You log the removal of a file before attempting the removal, but if rm fails, your log will not contain a record of that failure. You could redirect the find command's standard error to the same logfile so that rm will inherit it? Or, if not, place the -exec before the -ls, so that if the rm fails the file won't be listed as removed in the log. You can then log the find/rm error using the script's stderr. Just a thought.

Regards,
Alister
Brilliant. It worked.

I understand your comment about logging but wouldnt it complicate it further? I might give it a try when i have too much time.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Beginners Questions & Answers

Grep error "No such file or directory" not solved

Dear all, I run a simple command: grep -f GTEx_snps.txt chr1_r2.txt>chr1_r2_GTEx.txt and got error: "chr1_r2_GTEx.txt: No such file or directory" while "chr1_r2_GTEx.txt is an non-existent file. I google searched some solutions and tried to add -s and --no-messages option grep -s... (2 Replies)
Discussion started by: forevertl
2 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

"Cannot Execute" script, SunOS. [solved]

I have a script in Server A that will run a script in ServerB. #!/bin/ksh/ ssh user@server "/path/script.sh" The script permissions are as follow: -rwxrwxrwx 1 user dba 75 Jun 11 10:00 script.sh I checked the existence of 'ksh' in /bin and its there. (bash isnt) ... (0 Replies)
Discussion started by: RedSpyder
0 Replies

5. Homework & Coursework Questions

[Solved] Shell script "whoson" sorta...

Shell script "whoson" Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: write a script that will tell you who is logged in the unix system here by using the... (1 Reply)
Discussion started by: csharp100
1 Replies

6. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question