In order to capture the error output from a command in bash shell and use it further in a bash shell you can you the following
COMMAND_ERROR=$( { command_with_error_output; } 2>&1 )
In order to capture the error output from a command in bash shell and use it further in a bash shell you can you the following
COMMAND_ERROR=$( { command_with_error_output; } 2>&1 )
Use the following to convert a timestamp to a date in Linux bash
date -d @1636995741 Mo 15 Nov 2021 18:02:21 CET
Using the asdf version manager to manage versions of various binaries like terraform, vault or packer is easily done by following the instructions below:
asdf plugin add terraform asdf install terraform latest asdf global terraform 0.14.9 asdf list terraform 0.14.9 terraform --version Terraform v0.14.9
If you would like to be able to see the dmesg output with timestamps instead of time in seconds since booting up, use the -T like:
dmesg -T | less
You would like to type some sensitive information in the command line (password, key), but you don’t want this to appear in the history.
Add a single space before the start of the command.
To restart your X Window Server from the command line in linux, without affecting any non-graphical programs (but losing any unsaved information in your graphical user interface programs), you can use the following:
sudo systemctl restart display-manager
Problem
You would like to be able to automatically send some emails (ie reports) from a linux server, using a bash script that runs in cronjob, without installing an email server on the linux server.
Solution
#!/bin/bash # Email setup SENDGRID_API_KEY="your_sendgrid_api_key" FILENAME_ATTACH="title_of_your_attachment" FILENAME_ZIP="the_path_to_your_zip_file" FILENAME_BASE64_TMP="the_path_to_temporary_base64_encoding" EMAIL_TO="email_to_address" EMAIL_SUBJECT="email_subject" EMAIL_FROM="email_from_address" EMAIL_MESSAGE="your_email_message" function email_exports() { FILENAME_BASE64=$(base64 -w0 $FILENAME_ZIP); REQUEST_DATA='{"personalizations": [{ "to": [{ "email": "'"$EMAIL_TO"'" }], "subject": "'"$EMAIL_SUBJECT"'" }], "from": { "email": "'"$EMAIL_FROM"'" }, "content": [{ "type": "text/plain", "value": "'"$EMAIL_MESSAGE"'" }], "attachments": [{ "content": "'"$FILENAME_BASE64"'", "filename": "'"$FILENAME_ATTACH"'" }] }'; # We need to store the base64 locally as the text # is too big for sending directly with curl echo $REQUEST_DATA > $FILENAME_BASE64_TMP curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \ -H "Authorization: Bearer $SENDGRID_API_KEY" \ -H "Content-Type: application/json" \ -d "@$FILENAME_BASE64_TMP"; rm $FILENAME_BASE64_TMP } email_exports
Problem
You have defined a variable in a bash script (ie MY_FOLDER) but you also want to dynamically get the parent folder (ie MY_FOLDER can have multiple values).
Solution
Use the dirname as in the following
MY_FOLDER=/home/user/first_folder/second_folder MY_PARENT_FOLDER=$(dirname $MY_FOLDER) echo $MY_FOLDER echo $MY_PARENT_FOLDER ....... /home/user/first_folder/second_folder /home/user/first_folder
Problem
You would like to remove some lines from the bash history (ie using cut and paste with wrong values).
Solution
You can use the following, taken from the answer here
for i in {1..no_of_lines}; do history -d start_line; done
where start_line is the start of the lines you want to remove and no_of_lines is the number of lines you want to be removed.
Problem
You would like to send the error output from a command or a script to both an error file and an email.
Solution
By using tee, you could do the following:
command_with_parameters 2>&1 | tee /log_file_name | mail -s 'Command error' me@mydomain.com