My process creates file like
abc.20090427.txt i.e abc.date.txt
next time when my process it has to detect if any previous "abc" exist.
If exist then move to archive and create a new abc file.
I am using test command but it doesnt allow wild card.
if [[ -f abc.*.txt ]]
then
mv abc.*.txt archive/abc.*.txt
fi
case statement will not work in this scenario since it cannot detect whether the file "abc" already exists.
Appreciate your help
That is not the test command; the test command is test or [.
is_file() { [ -f "$1" ]; }
if is_file abc.*.txt
then
mv abc.*.txt archive/
fi
In my directory i have
abc.txt
def.txt
#!/bin/ksh
is_file() { [ -f "$1" ]; }
if is_file *.txt
then
mv *.txt archive
fi
when i run the above code the files are still there
What is the output of this script:
is_file() { [ -f "$1" ]; }
printf "%s\n" *.txt
if is_file *.txt
then
mv *.txt archive
fi
printf "%s\n" *.txt
This version of is_file is a little more robust:
is_file() {
for f; do
[ -f "$f" ] && return
done
return 1
}
Tried both doesnt gives any output.
I have two files with .txt at ending but they are not moved
#!/bin/ksh
is_file() {
for f; do
[ -f "$1" ] && return
done
return 1
}
printf "%s\n" *.txt
if is_file *.txt
then
mv *.txt archive
fi
printf "%s\n" *.txt
#!/bin/ksh
is_file() {
for f; do
[ -f "$f" ] && return
done
return 1
}
printf "%s\n" *.txt
if is_file *.txt
then
mv *.txt archive
fi
printf "%s\n" *.txt
~
~
If there is no output, you don't have any *.txt files in the current directory.
Are you running the script in a different directory?
I am running in the directory where i have two .txt files
ABC.txt
DEF.txt
Script runs no output and files are not moved
$ksh -x test
+ + basename test
cmd=test
+ test
If there is no output, then there are no *.txt files in the directory, or you are not running the code I gave you.
What is the result of this command typed at the command line in the same directory where you ran the script:
pwd
ls -l *.txt
Now, what is the result of these commands, also run at the command prompt in the same directory:
pwd
ls -l *.txt
mv *.txt archive
ls -l *.txt
Don't give your scripts the same name as a standard command; test is both a shell builtin and an external command.