The goal is to check the existence of a file and create a blank file if this doesn't exist, using Shell Script on the Pre-session-command (Informatica PowerCenter) like the code below:
ParamDirTrabalho=/dir/powercenter/project1
ParamArq=file.csv
ParamQtdArq=`cat ${ParamDirTrabalho}/${ParamArq} | wc -l`
if [ $ParamQtdArq == 0 ];then touch ${ParamDirTrabalho}/${ParamArq};fi
This is the error:
Message: [Pre/Post Session Command] Process id 10683. Standard output and error:
sh: line 2:
: command not found
cat: /dir/powercenter/project1
/file.csv
: No such file or directory
sh: line 4:
: command not found
I can execute successfully when pointing to a sh file with the code above. But I need to write the code inside the pre-session-command.
Please enclose parameters by double quotes.
ParamDirTrabalho="/dir/powercenter/project1"
ParamArq="file.csv"
Also pls make sure you provide RWX permission to folders.
You cannot get the WC from a file if it doesn't exist at all. That's what the error is "No such file or directory" if I understand it right. What you need to do is check if file exists or not rather than the count and then touch if it doesn't exist.
if [ ! -f filename ];then touch filename; fi
or
if [ -f filename ];then exit 0; else touch filename; fi
Related
i have one command in shell script which counts number of lines in file and if they are zero then continue to execute rest of the script.
Example :
v_cnt=`wc -l $1/test_$3.bad`
if v_cnt > 0
then
exit 1
fi
File gets generate dynamically.
issue i am facing is , when file does not get generated , i get Permission denied error. However it works properly when file is present. please help me for the same.
Try this. Rather than using wc -l, try using the else condition in if [ -s ].
if [ -e $1/test_$3.bad ]
then
if [ -s $1/test_$3.bad ]
then
exit 1
else
do something
fi
else
echo "File was not generated"
fi
I want to check if a file or directory with same name exists.
Is there any operator (expect -e) to check the file or directory, I dont want to add any extra condition.
Following code works fine to check the existence of a file:
#!/bin/bash
if [ -e /path/to/the/file/sample ]
then
echo "ok"
else
echo "nok"
fi
Note that sample can be a file or a directory.
In my tests with bash 4.2 , the condition [ -e /path/to/the/file/sample ] works for BOTH files and directories.
As an alternative you can :
Either to use another operator like -d according to bash man page.
if [ -e /path/to/the/file/sample ] || [ -d /path/to/the/file/sample ];then echo "ok"; else echo "nok";fi
Or to use the exit code of a command like ls.
if ls "/path/to/the/file/sample" &>/dev/null ; then echo "is here";else echo "not here";fi
If you try to perform ls on an existed file or directory ls will return 0, otherwise will return a non-zero value (returns 2 in my tests for non-existed files or directories).
Quoting is necessary to handle correct names that may include spaces.
I have a bash script which creates a directory if not already present and moves all the files to the newly created directory.
The bash script I have is returning is not working and the error is receive is
./move.sh: line 5: =/data/student/stud_done_11-11-2013: No such file or directory
already present
mv: missing destination file operand after `a.xml'
Try `mv --help' for more information.
The bash script is:
# Back up
if [ $# = 1 ]
then
$dir="/data/student/stud_done_$1"
echo $dir
if [ ! -d $dir ]; then
mkdir $dir
else
echo "already present"
fi
cd /data/student/stud_ready
mv * $dir
else
echo "No files to move"
fi
I invoke the script as follows:
./move.sh "11-11-2013"
What is the error in my script.
Here (on line 5)...
$dir="/data/student/stud_done_$1"
You meant...
dir="/data/student/stud_done_$1"
I need to write a basic program which would find the files which have odd (uneven) size in bytes in user specified directory and then rename them. I wrote a code but can't figure it out what's wrong with it since I have only just began to programm bash scripts... I have 3 files in my directory and here are the errors I'am getting for them:
./Untitled: line 18: AppIcon.icns: command not found
mv: cannot stat ‘AppIcon.icns’: No such file or directory
./Untitled: line 18: AssociatedVm.txt: command not found
mv: cannot stat ‘AssociatedVm.txt’: No such file or directory
./Untitled: line 18: Info.plist: command not found
mv: cannot stat ‘Info.plist’: No such file or directory
My script Code:
#!/bin/bash
n=0
echo “Specify directory”
read directory
if [ -d $directory ]; then
echo “Directory found”
else
echo “Directory not found”
exit 0
fi
for file in $( ls $directory );
do
fsize=$(stat "$directory/$file" -c %s)
if [ $((fsize%2))=1 ]; then
mv "$directory/$file" "$directory/$file.odd"
n=$((n + 1))
fi
done
echo ”Number of renamed files: $n ”
I think you meant
fsize=$(stat "$file" -c %s)
but you wrote
fsize=stat "$file" -c %s
Also, you need to use the absolute path($directory/$file) instead of $file alone if you are running the script from a directory which is not $directory.
Bash uses -eq for integer comparison, so you should also change
if [ $((fsize%2))=1 ]; then
to
if [ $((fsize%2)) -eq 1 ]; then
What is the -c %s for? I don't see a -c option in the stat man page. Did you mean -f? (EDIT: Ok I was looking at the Mac stat command (which is BSD). The stat in GNU version uses -c for format specification)
In a (ba)sh script, how do I ignore file-not-found errors?
I am writing a script that reads a (partial) filename from stdin, using:
read file; $FILEDIR/$file.sh
I need to give the script functionality to reject filenames that don't exist.
e.g.
$UTILDIR does NOT contains script.sh
User types script
Script tries to access $UTILDIR/script.sh and fails as
./run.sh: line 32: /utiltest/script.sh: No such file or directory
How do I make the script print an error, but continue the script without printing the 'normal' error?
You can test whether the file exists using the code in #gogaman's answer, but you are probably more interested in knowing whether the file is present and executable. For that, you should use the -x test instead of -e
if [ -x "$FILEDIR/$file.sh" ]; then
echo file exists
else
echo file does not exist or is not executable
fi
if [ -e $FILEDIR/$file.sh ]; then
echo file exists;
else
echo file does not exist;
fi
Here we can define a shell procedure that runs only if the file exists
run-if-present () {
echo $1 is really there
}
[ -e $thefile ] && run-if-present $thefile
Depending on what you do with the script, the command will fail with a specific exit code. If you are executing the script, the exit code can be 126 (permission denied) or 127 (file not found).
command
if (($? == 126 || $? == 127))
then
echo 'Command not found or not executable' > /dev/stderr
fi