[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.
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)