Author: Sahil Suri

Shell scripting: watch command alternative via while loop

We may frequently find ourselves in situations where we want to run a command periodically to view its progress. A watch command is an amazing tool meant to be used for this exact purpose. Please read our article on the Linux watch command to know more about it. In this article, we’ll explain how you may use the while loop in a bash shell to simulate the functionality provided by the watch command. The syntax is simple as shown below: while true; do echo "Running <command> at $(date)"; <command>; sleep <interval in sec> ; done Explanation of the above shell script The true keyword turns the while loop into an infinite loop since the exit status or condition to be checked by the while loop will always literally be true. This is followed by the “; operator” which is useful to concatenating/chaining commands in Linux/Unix. Next, we display the name of the command we’ll be executing and $(date) performs a command substitution for the date command. This is followed by the actual command to be executed. Finally, we have the Linux sleep command where we specify the time in seconds after which the next iteration of the while loop should run and display the output of the command. The done statement closes the while loop. You may terminate the while loop by pressing ctrl+c. Example: We’ll now demonstrate a simple...

Read More

Using .gitignore to prevent files from being tracked

Introduction While working on our projects and using a version control system we could create and make use of a wide variety of different types of files. We may not want to keep track of every single file in our repository. Under such circumstances, we could use the .gitignore file to prevent certain files or certain file patterns from being tracked by git. In this article, we will demonstrate a practical scenario where we would need to prevent certain files from being tracked and we will make use of the .gitignore file to fulfill this requirement. Demonstration: While working on a project you would probably have related files placed in some folders within your repository instead of having all the files jumbled up into a single folder. You might have an src folder for source code, a docs folder for documentation, an output folder to store program output etc. Within my repository, I currently have only one file and no folders. [sahil@linuxnix test_repo]$ pwd /home/sahil/test_repo [sahil@linuxnix test_repo]$ git status # On branch master nothing to commit, working directory clean [sahil@linuxnix test_repo]$ ls help.txt [sahil@linuxnix test_repo]$ So, let’s create some sub-directories within this directory. [sahil@linuxnix test_repo]$ mkdir src docs output [sahil@linuxnix test_repo]$ If we run the git status command again we notice something strange. [sahil@linuxnix test_repo]$ git status # On branch master nothing to commit, working directory clean [sahil@linuxnix test_repo]$...

Read More

git: What is detached HEAD state?

Introduction In our previous article on working with the git version control system, we explained how to use the git checkout command to retrieve previously committed versions of files from the git repository. In this article, we will talk about a situation that arises if you don’t mention a file name while running the git checkout command and simply type ‘git checkout HEAD~n’ where n is the commit to which to want to move the head to. How does a repository go into a detached HEAD state? Using the git checkout command we generally check out a branch of the repository to work with the content within that branch. We’ll talk about branches in detail in a separate article.  For the time being, we’ll define branches as movable pointers to commits in the version history of our git repository. When we initialize a git repository we work with a default branch named master. We may create further branches and subsequently work with those branches. Let’s take a look at the state of the files inside the repository we’ve been using four commits before the current committed state. [sahil@linuxnix my_first_repo]$ git diff HEAD~4 diff --git a/README.md b/README.md deleted file mode 100644 index ba76a74..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -This is a readme file for my first git repository diff --git a/test.txt b/test.txt index c66d471..e945f40 100644 --- a/test.txt +++...

Read More

Over 16,000 readers, Get fresh content from “The Linux juggernaut”

Email Subscribe

ABOUT ME..!

My photo
My name is Surendra Kumar Anne. I hail from Vijayawada which is cultural capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. At present I work at Bank of America as Sr. Analyst Systems and Administration. You can contact me at surendra (@) linuxnix dot com.