Permission denied while executing shell script - shell

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

Related

How to check if file exists in Google Cloud Storage with the gcloud bash command?

I need to check if a file exists in a gitlab deployment pipeline. How to do it efficiently and reliably?
Use gsutil ls gs://bucket/object-name and check the return value for 0.
If the object does not exist, the return value is 1.
You can add the following Shell script in a Gitlab job :
#!/usr/bin/env bash
set -o pipefail
set -u
gsutil -q stat gs://your_bucket/folder/your_file.csv
PATH_EXIST=$?
if [ ${PATH_EXIST} -eq 0 ]; then
echo "Exist"
else
echo "Not Exist"
fi
I used gcloud cli and gsutil with stat command with -q option.
In this case, if the file exists the command returns 0 otherwise 1.
This answer evolved from the answer of Mazlum Tosun. Because I think it is a substantial improvement with less lines and no global settings switching it needs to be a separate answer.
Ideally the answer would be something like this
- gsutil stat $BUCKET_PATH
- if [ $? -eq 0 ]; then
... # do if file exists
else
... # do if file does not exists
fi
$? stores the exit_status of the previous command. 0 if success. This works fine in a local console. The problem with Gitlab will be that if the file does not exists, then "gsutil stat $BUCKET_PATH" will produce a non-zero exit code and the whole pipeline will stop at that line with an error. We need to catch the error, while still storing the exit code.
We will use the or operator || to suppress the error. FILE_EXISTS=false will only be executed if gsutil stat fails.
- gsutil stat $BUCKET_PATH || FILE_EXISTS=false
- if [ "$FILE_EXISTS" = false ]; then
... # do stuff if file does not exist
else
... # do stuff if file exists
fi
Also we can use the -q flag to let the command stats be silent if that is desired.
- gsutil -q stat $BUCKET_PATH || FILE_EXISTS=false

ShellScript not working on Pre-session-command (PowerCenter)

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

console shows "unknown operand" from a if statement

I'm trying to script an automatic md5sum check for my embedded system running uClinux.
The script is generated on my computer as well as the tar file I want to check.
The script goes like this :
#!/bin/sh
filename='My_File'
md5='d4deeac6f655ee5d4b9ec150fc6957a5'
if test ! -e $filename.tar
then
echo Update file does not exist
exit 1
fi
if [ -z `md5sum "$filename.tar" | grep $md5` ]
then
echo 'md5sum is not correct'
exit 1
else
echo 'md5sum is correct'
fi
tar -xvf "$filename.tar"
[...]
The md5sum check run as expected, i-e the script stops when the checksum is wrong and executes to the end otherwise. But when the checksum is correct, I get this message from the console :
[: My_File.tar: unknown operand
I don't understand why I get this, and I think this is not accurate to let my script like this. Can someone explain me what's the shell is doing and how to get rid of this message ?
Thanks
Quote the output of md5sum so it's not split into multiple words. -z only expects one operand.
if [ -z "`md5sum "$filename.tar" | grep $md5`" ]
While we're here, might as well switch to the nicer $(...) syntax.
if [ -z "$(md5sum "$filename.tar" | grep $md5)" ]

Unix shell script to check if a file or directory with same name exists

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.

How to get bash to ignore file-not-founds

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

Resources