Error in Bash script to check existing file in Solaris - oracle

im gong to compile oracle forms on Solaris and create a script.
the script should check if .fmx is created then removes .err file.
here is my script but I've received below error
Code to remove error files
export FORMS_PATH=export FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
for FILE in `ls $FORMS_PATH/*.fmx`; do
if exist "$FILE/*.fmx";
then
rm $FILE/err
fi
done
Error Encountered
rmerr.sh[3]: exist: not found [No such file or directory]

Regular File test is done using "-f"
export FORMS_PATH=export FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
for FILE in `ls $FORMS_PATH/*.fmx`; do
# True if file exists and is a regular file.
if [ -f "$FILE/*.fmx"]; then
rm $FILE/err
fi
done

This might be what you want to do, but it is unclear where .fmx and .err files are located:
export FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
for FILE in $FORMS_PATH/*.fmx; do
b=$(basename $FILE)
[ -f "$b" ] && rm ${b%fmx}err
done

".err" is a file, but you list "err" here.
Some other problem here:
export FORMS_PATH=export FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
Replace with "FORMS_PATH=/apps/apps/frmcompile/cmteam/hla"
for FILE in ls $FORMS_PATH/*.fmx; do
FILE contains every file ending in ".fmx"
if exist "$FILE/.fmx";
Result eg in "/apps/apps/frmcompile/cmteam/hla/blaba.fmx/.fmx" with shell expansion and "exist" - what's this - try "test" or "[]".
rm $FILE/err
Results in "/apps/apps/frmcompile/cmteam/hla/blaba.fmx/err or .err in subfolder and that you don't like, or?
So best use this:
#!/bin/sh OR #!/bin/bash
FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
for fmx in $FORMS_PATH/*.fmx; do
# remove your files ending in .err instead of .fmx
/bin/rm "${fmx%.fmx}.err # only valid with bash
done
Tom

Related

Create new Directories with Bash Script

I'd like to create a script to run every hour (with crontab) to make a folder with the name of any file with the correct extension (minus the extension) and move that file into it. So the end result would be the script would execute, find every .mp4 file in /Directory, create a folder for each of them with the same name as the file (minus extension) in /Other/Directory, and move the file into the matching folder. I can understand not wanting to write something for someone for free, but if you could point me in the right direction, I would really appreciate it.
EDIT: Thanks to #Barmar for the help!
#!/bin/bash
cd "/home/kali/Videos"
for FILE in *;do
bn=$(basename $FILE .mp4)
mkdir /home/kali/Videos/$bn;done
mv $bn.mp4 /home/kali/Videos/$bn
The script you would be looking for is as follows:
#!/bin/bash
REPOSITORY="/home/kali/Videos"
cd "${REPOSITORY}"
### This approach is best for handling filenames that might have spaces or scpecial characters.
ls |
while [ true ]
do
read FILE
if [ -z "${FILE}" ] ; then break ; fi
if [ -f "${FILE}" ]
then
bn=`basename "${FILE}" ".mp4" `
mkdir "${REPOSITORY}/$bn"
mv "${FILE}" "${REPOSITORY}/$bn"
( cd "${REPOSITORY}/$bn" ; extract_images "./${FILE}" )
fi
done

How to check if a file is in a dir and then delete it and another file?

I'm now using Ubuntu, and increasingly using terminal.
I would like to delete files from Trash via command line.
So, I've gotta delete files from ~/.local/share/Trash/files dir.
All right, here's the question:
When I move some file to trash, it also creates a file_name.trashinfo file in ~/.local/share/Trash/info.
How could I automatically delete the corresponding .trashinfo file when I delete something in ../files?
You can use the following script to delete both files simultaneously. Save it in some file in the ~/.local/share/Trash directory, and call then bash <script.sh> <path-to-file-to-be-deleted-in-files-dir>.
A sample call to delete the file test if you named the script del.sh: bash del.sh files/test
#!/bin/bash
file=$1
if [ -e "$file" ] # check if file exists
then
rm -rf "$file" # remove file
base=$(basename "$file")
rm -rf "info/$base.trashinfo" # remove second file in info/<file>.trashinfo
echo 'files deleted!'
fi

shell script to remove a file if it already exist

I am working on some stuff where I am storing data in a file.
But each time I run the script it gets appended to the previous file.
I want help on how I can remove the file if it already exists.
Don't bother checking if the file exists, just try to remove it.
rm -f /p/a/t/h
# or
rm /p/a/t/h 2> /dev/null
Note that the second command will fail (return a non-zero exit status) if the file did not exist, but the first will succeed owing to the -f (short for --force) option. Depending on the situation, this may be an important detail.
But more likely, if you are appending to the file it is because your script is using >> to redirect something into the file. Just replace >> with >. It's hard to say since you've provided no code.
Note that you can do something like test -f /p/a/t/h && rm /p/a/t/h, but doing so is completely pointless. It is quite possible that the test will return true but the /p/a/t/h will fail to exist before you try to remove it, or worse the test will fail and the /p/a/t/h will be created before you execute the next command which expects it to not exist. Attempting this is a classic race condition. Don't do it.
Another one line command I used is:
[ -e file ] && rm file
You can use this:
#!/bin/bash
file="file_you_want_to_delete"
if [ -f "$file" ] ; then
rm "$file"
fi
If you want to ignore the step to check if file exists or not, then you can use a fairly easy command, which will delete the file if exists and does not throw an error if it is non-existing.
rm -f xyz.csv
A one liner shell script to remove a file if it already exist (based on Jindra Helcl's answer):
[ -f file ] && rm file
or with a variable:
#!/bin/bash
file="/path/to/file.ext"
[ -f $file ] && rm $file
Something like this would work
#!/bin/sh
if [ -fe FILE ]
then
rm FILE
fi
-f checks if it's a regular file
-e checks if the file exist
Introduction to if for more information
EDIT : -e used with -f is redundant, fo using -f alone should work too
if [ $( ls <file> ) ]; then rm <file>; fi
Also, if you redirect your output with > instead of >> it will overwrite the previous file
So in my case I wanted to remove a FIFO file before I create it again, so this worked for me:
#!/bin/bash
file="/tmp/test"
rm -rf $file | true
mkfifo $file
| true will continue the script even if file is not found.

how to check if a file format existing in a particluar folder using shell

I want to use a if condition to delete records from the table.
eg :
if ()
then
delete from tablename where filecode like '%A%';
commit;
end if;
the if condition is to check or a particular file format
so there is a possibility of 3 file formats say (A.csv,B.CSV,C.CSV)to arrive in the /a/b/input directory
the column filcode in the table will hold the filename from where the record is obtained.
The above script will run in the location /a/b/scripts directory
Perhaps what you simply want is a script like this. It checks if a given file exists.
#!/bin/sh
file=$1
if [ -e "$file" ]; then
echo "File $file exists."
fi
Or simply
#!/bin/sh
[ -e "$1" ] && echo "File $1 exists."
Save the script in UNIX format in /a/b/scripts like /a/b/scripts/check_file_exists.sh, and run:
sh /a/b/scripts/check_file_exists.sh /a/b/input/somefile.ext
The unix program to check for file formats is called file. To see all the file formats (listed as mime-types) in the directory /a/b/input, run:
file -L --mime-type /a/b/input/*
If you want to select only the files in that directory that are have the jpeg file format, run:
file -L --mime-type /a/b/input/* | grep 'image/jpeg'
try this:
#!/bin/sh
if [ -f /a/b/input ]; then
sh /a/b/scripts/your-script
fi
or this:
test -f /a/b/input && sh /a/b/scripts/your-script

Quick bash script to run a script in a specified folder?

I am attempting to write a bash script that changes directory and then runs an existing script in the new working directory.
This is what I have so far:
#!/bin/bash
cd /path/to/a/folder
./scriptname
scriptname is an executable file that exists in /path/to/a/folder - and (needless to say), I do have permission to run that script.
However, when I run this mind numbingly simple script (above), I get the response:
scriptname: No such file or directory
What am I missing?! the commands work as expected when entered at the CLI, so I am at a loss to explain the error message. How do I fix this?
Looking at your script makes me think that the script you want to launch a script which is locate in the initial directory. Since you change you directory before executing it won't work.
I suggest the following modified script:
#!/bin/bash
SCRIPT_DIR=$PWD
cd /path/to/a/folder
$SCRIPT_DIR/scriptname
cd /path/to/a/folder
pwd
ls
./scriptname
which'll show you what it thinks it's doing.
I usually have something like this in my useful script directory:
#!/bin/bash
# Provide usage information if not arguments were supplied
if [[ "$#" -le 0 ]]; then
echo "Usage: $0 <executable> [<argument>...]" >&2
exit 1
fi
# Get the executable by removing the last slash and anything before it
X="${1##*/}"
# Get the directory by removing the executable name
D="${1%$X}"
# Check if the directory exists
if [[ -d "$D" ]]; then
# If it does, cd into it
cd "$D"
else
if [[ "$D" ]]; then
# Complain if a directory was specified, but does not exist
echo "Directory '$D' does not exist" >&2
exit 1
fi
fi
# Check if the executable is, well, executable
if [[ -x "$X" ]]; then
# Run the executable in its directory with the supplied arguments
exec ./"$X" "${#:2}"
else
# Complain if the executable is not a valid
echo "Executable '$X' does not exist in '$D'" >&2
exit 1
fi
Usage:
$ cdexec
Usage: /home/archon/bin/cdexec <executable> [<argument>...]
$ cdexec /bin/ls ls
ls
$ cdexec /bin/xxx/ls ls
Directory '/bin/xxx/' does not exist
$ cdexec /ls ls
Executable 'ls' does not exist in '/'
One source of such error messages under those conditions is a broken symlink.
However, you say the script works when run from the command line. I would also check to see whether the directory is a symlink that's doing something other than what you expect.
Does it work if you call it in your script with the full path instead of using cd?
#!/bin/bash
/path/to/a/folder/scriptname
What about when called that way from the command line?

Resources