Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Exit Shell script with error if any command fails

 

 

I wrote a Bash script today to automate my code review process and I noticed that even when certain commands in the workflow fail, the script still executes the remaining commands.

 

For example, let's say I have two commands that run synchronously:

 

git merge origin/dev git tag <new-tag>

 

In this case, if the `git merge` command fails due to some reason, it will still create a new tag, which is not my intended workflow.

 

To avoid this, if any of the commands returns a non-zero exit status, the script should break and exit. This can be achieved in Bash by using the `-e` option.

 

Add the following at the very top of your script (below the shebang line):

 

#!/bin/bash set -e

 

This will enable the `-e` option for the entire script.

If you only want to enable this option for specific commands, you can prefix them with `set -e`, like this:

 

set -e git push

 

This will cause the script to exit if the `git push` command fails.

 

Clear scroll back buffer in zsh




In bash you can use ctrl + L to clear the scroll back in the terminal. However in zsh, it doesn't work. So here is a workaround. You can use a control sequence to clear it, and set an alias for that. 

 

Open your  .zshrc file in the editor.

$ vim ~/.zshrc

And add this to the file.

alias cls='printf "\ec\e[3J"'

Save and reload the terminal: or

    $ source ~/.zshrc

 

Now when you run the command 'cls', it should clear your terminal with its scroll back buffer. Hope it helps. 



How to turn on Android device without the power button

How to turn on Android device without the power button 

 

I broke the power button in my Android phone and I was keeping it alive by constantly charging it (so the battery won't die and I won't lose any data in my phone in case it get switched off).

 

But when I woke up today morning, I realized I forgot to connect my charger last night, and now my phone is gone forever :( But I wasn't ready to give up it so easily. So I looked for a way to "revive" this dead phone :D and luckily I found some resources which I tried (all of them) and here's what worked for me. 

 

First you need some charge on your phone. Connect it to a charger for a while.

 

Then you need to start your phone in the fast boot mode. This is easy. Just hold your volume down button (or up button? try.) and connect your usb cable while you're holding that volume button. Make sure you actually connect the other end of the cable to your computer as well. If it goes well, your phone should power up and should take you to the fast boot screen. Now its kinda powered on, yay!

 

Now keep it like that and come back to your computer. You need android platform tools. Head over to the following page and download them to your computer. Pick the right one for your OS.

 

https://developer.android.com/studio/releases/platform-tools

 

 Extract the files and copy it to somewhere in your computer. I kept these files in the following folder. 

 

/Users/nimeshkasrimal/Documents/adb/platform-tools

 

Now you need to add this path to your PATH system variable.

 

echo 'export PATH=$PATH:/Users/nimeshkasrimal/Documents/adb/platform-tools/' >> ~/.bash_profile

 

and then 


source ~/.bash_profile

 

to update your current terminal session with the new path. (If you're on Windows, this is a bit different, just Google it and you will find how to do this)

 

Alright, now you are ready. Just two commands away. 

 

Now if you run 

 

fastboot devices

 

It should list your phone in the command output. (If it doesn't show your phone, reconnect it the computer and run the command again.)

 

~/Documents/adb/platform-tools> fastboot devices
84RDU17815002927    fastboot

 

and now, all you need to do is, run the following command.

 

fastboot reboot

~/Documents/adb/platform-tools> fastboot reboot
Rebooting                                          OKAY [  0.022s]
Finished. Total time: 0.022s


This should reboot your phone and you're done! :) I hope this blog post will help someone. Please leave a comment and let me know if you have any problems, suggestions :)