I'm trying to get the value of a resource in azure via AZ CLI, and pass that value to a variable in bash.
id=$(az synapse workspace show -n $name -g $rsname --query 'identity.principalId' -o tsv 2<&1)
if [[ $id == *"Not Found"* ]];
then
echo "Workspace already deleted."
fi
If the resource is not there, I am redirecting the output to the variable with 2<&1 so I can deal with it in the if-then conditional. $id is getting assigned the output correctly, but AZ CLI is still exiting the script with error "not found".
Is there anyway to keep it from exiting?
In your bash command you are using 2<&1 that's why it exited the script with error "not found"
You can achieve this by using "2>&-".
Make sure you have to use the Greater than (>) symbol.
id=$(az synapse workspace show -n '<synapse Name>' -g '<Resource Group Name>' --query 'identity.principalId' -o tsv 2>&-)
using "2>&-":
I can be able to get the principal id.
using "2>&1":
here I am not able to get the principal id.
Reference
using bash check the program is exist or not
Usage of bash IO Redirection
Related
I am creating a bash script to automate certain actions in my cluster. One of the commands is: kubectl delete -f example.yaml.
The problem is that when the resources defined in the YAML do not exist, the following error is printed:
Error from server (NotFound): error when deleting "example.yaml": deployments.apps "my_app" not found
I am looking to add an additional step that first checks whether a set of resources defined in a YAML file exist in the cluster. Is there a command that would allow me to do so?
From the documentation, I found:
Compares the current state of the cluster against the state that the cluster would be in if the manifest was applied.
kubectl diff -f ./my-manifest.yaml
but I find it difficult to parse the output that it returns. Is there a better alternative?
To find out if the same object is already present in the cluster as exactly described in the manifest file. you can use the return code of the kubectl diff command.
Exit status:
0 No differences were found.
1 Differences were found.
>1 Kubectl or diff failed with an error.
Example:
kubectl diff -f crazy.yml &>/dev/null
rc=$?
if [ $rc -eq 0 ];then
echo "Exact Object is already installed on the cluster"
elif [ $rc -eq 1 ];then
echo "Exact object is not installed, either its not installed or different from the manifest file"
else
echo "Unable to determine the difference"
fi
Alternatively, if you want to really parse the output, you may use the following env variable to print the diff output in desired format:
KUBECTL_EXTERNAL_DIFF environment variable can be used to select your
own diff command. Users can use external commands with params too,
I want to assign a variable in shell script for the below aws command..
If the command is successful,I want to assign the output to S3_BUCKET_REGION.Eg: S3_BUCKET_REGION = us-east-1.
S3_BUCKET_REGION=$( aws s3api get-bucket-location --bucket ${TF_STATE_S3_BUCKET} | jq -r '.LocationConstraint // "us-east-1"' )
But if the bucket does not exist,the error for the above command is "An error occurred (NoSuchBucket) when calling the GetBucketLocation operation: The specified bucket does not exist"
I want to capture this error and echo it in the script.
So if the command runs successfully,I want to assign to a variable.If not ,I want to echo the error.How to do conditional statement for this?
Usually commands sends output to STDOUT and errors to STDERR.
$() grabs only STDOUT, so you should finish your command with redirection of STDERR to STDOUT
MYVAR=$( blablabla 2>&1 )
I am executing a kubectl command in a bash script and storing the output in a variable. When the kubectl command executes successfully I am getting the correct output in the variable, but when it does not execute successfully the variable is empty and the error message is not available in the variable. I want the error values to be stores in the variable.
Example:
GET_PODS_COMMAND="$(kubectl get pods -n mlsh-$JOBNAMESPACE --selector app=$POD_SELECTOR --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')" #kubectl command. Please assume mlsh-$JOBNAMESPACE and $POD_SELECTOR have correct values
GET_PODS_COMMAND_OUT=$GET_PODS_COMMAND
echo $GET_PODS_COMMAND_OUT #Printing command result
When the command execution is successful I get the pod name in GET_PODS_COMMAND_OUT but when the command output is "No Resources Found" value for GET_PODS_COMMAND_OUT is blank.
I read that I have to redirect the stderr to stdout as stated in below articles:
Bash how do you capture stderr to a variable?
https://www.reddit.com/r/kubernetes/comments/98s8v6/why_cant_i_store_outputs_of_kubectl_commands_in_a/
Still struggling to understand how exactly to achieve this.
Here is what I have tried:
GET_PODS_COMMAND_OUT="$(GET_PODS_COMMAND 2>&1)" #gives the error: GET_PODS_COMMAND: command not found
New to linux so any help is appreciated. Thank you.
I have a bash script running bunch of stuff along with aws listobject command. Recently we noted that the std err would have an error code but the script does not exit with non zero code. I hoped that the set -e should handle any command failures but it doesnt seem to do the trick.
Script looks like this:
#!/bin/bash
set -e
# do stuff
aws s3api list-objects --bucket xyz --prefix xyx --output text --query >> files.txt
# do stuff
Error in Stderr :
An error occurred (SlowDown) when calling the ListObjects operation (reached max retries: 4): Please reduce your request rate.
Objective:
I want the bash script to fail & exit when it encounters a problem with the aws cli commands. I can add an explicit check on ($? != 0) but wondering if there is better way to do this.
For me, this did the trick:
set -e -o pipefail
The #codeforrester's link says:
set -o pipefail is a workaround by returning the exit code of the first failed process
I have a shell script that executes multiple sql files that updates to the database. I am calling the shell script from jenkins- build- execute shell. The jenkins console shows success at all times irrespective of the errors from the sql files. I want Jenkins to fail the build, if there is an error or any of the sql file failed executing and send the console output to the developer, if fails.
I tried echo $? in the shell script but it shows 0.
#!/bin/bash
walk_dir () {
shopt -s nullglob dotglob
for pathname in "$1"/*; do
if [ -d "$pathname" ]; then
walk_dir "$pathname"
else
case "$pathname" in
*.sql|*.SQL)
printf '%s\n Executing SQL File:' "$pathname"
sudo -u postgres psql <DBName> -f $pathname
rm $pathname
esac
fi
done
}
DOWNLOADING_DIR=/home/jenkins/DB/
walk_dir "$DOWNLOADING_DIR"
Jenkins Console results
ALTER TABLE
ERROR: cannot change return type of existing
DETAIL: Row type defined by OUT parameters is different.
CREATE FUNCTION
ALTER FUNCTION
CREATE FUNCTION
ALTER FUNCTION
Finished: SUCCESS
Expected Results: Failed from Jenkins (if any of the sql files failed executing from shell script) but it is showing as passed in Jenkins
Thanks for all the inputs. I was able to fix this issue. I installed 'log parser plugin' in Jenkins which will parse the keywords like /Error/ in the console output and make the build to fail.
This S/O answer will probably address your scenario: Automatic exit from bash shell script on error
This duplicate answer also provides useful guidance:Stop on first error [duplicate]
Essentially use set -e or #!/bin/bash -e.
If you don't trap every potential error, then the next step in the script will execute and the return code will be that of the last command in the script.
Direct link to www.davidpashley.com - Writing Robust Bash Shell Scripts
** This also assumes any external commands (eg: psql) also properly trap and return status codes.
Below code may help you. This is how i sorted the issue with mine.
Instead of sudo -u postgres psql <DBName> -f $pathname
Use below code:
OUTPUT=$(psql -U postgres -d <DBName> -c "\i $pathname;")
echo $OUTPUT | grep ERROR
if [[ $? -eq 0 ]]
then
echo "Error while running sql file $pathname"
exit 2
else
echo "$pathname - SQL file Executed, successfully"
fi
Jenkins will not provide you the SQL files error code. Jenkins just checks if your shell script is executed or not and based on that it will return a status code which generally is zero as it is executing the shell script successfully.