Getting the hostname of the node running a program can be useful in various scenarios, such as creating logs with the hostname, identifying which node a script is running on, or configuring a distributed system with different nodes. In Bash, there are several ways to retrieve the hostname of the machine, as mentioned in the
Read more
Tag: Bash
How to get the epoch timestamp in Bash?
Posted onIn Bash, how to get the epoch timestamp, the number of seconds passed since the epoch? In Bash, you can call the date command with format option “+%s”: date +%s Here, “%s” means: %s seconds since 1970-01-01 00:00:00 UTC
How to get an environment variable in Bash?
Posted onIn Bash, how to get the value (string) of an environment variable? In Bash, just directly use: $VAR Note that if you assigned a value to the “local” variable $VAR in your program before reading the “environmental” $VAR, the $VAR will actually contain the value you assigned.
How to process a file line by line in Bash?
Posted onIn Bash, how to process a file line by line? The file is a plain text line like input.txt. As an example, the process can be to just print it out. In Bash to process a file ./input.txt line by line: while read line ; do echo $line done < ./input.txt Reference: https://www.systutorials.com/qa/2166/how-to-process-a-file-line-by-line-in-go
How to process a file line by line in Python?
Posted onIn Python, how to process read a file line by line to process it? Like those lines in Bash: while read line ; do echo $line done < ./input.txt In Python, you can process a file line by line by a for in the file like with open(“./input.txt”, “r”) as thefile: for line in thefile:
Read more
In Bash script, how to join multiple lines from a file?
Posted onIn Bash script, how to join multiple lines from a file? For example, to join the lines a good boy to a good boy You can use tr command, something like: tr -s ‘n’ ‘ ‘ < file.txt It just goes through its input and makes changes according to what you specify in two sets
Read more
How to make tee catch the stderr only in Linux?
Posted onI would like to make tee catch the stderr and log it into a file. A brute force method by let tee listen on both stderr and stdout is okay like cmd 2>&1 | tee -a log But how to make tee catch the stderr only? You can make use of “process substitution” (>(…)) to
Read more
How to handle spaces in paths with rsync on Linux?
Posted onThe common rsync commands seems not handle spaces well. For example, rsync -avxP file “user@server:/data/my dir” It reports: rsync: link_stat “/home/zma/file” failed: No such file or directory (2) How to make rsync handle spaces well? You can use the –protect-args option of rsync. $ rsync –protect-args -avxP file “user@server:/data/my dir” What does –protect-args do: -s,
Read more
How to make a unique temporary file in Bash on Linux?
Posted onIt is common to make a unique temporary file. How to make a unique temporary file in Bash on Linux safely and quickly? You can use the mktemp program. In the simplest way, tmpfile=$(mktemp) The file will be like /tmp/tmp.j0wD39Mr3b if you do not prefer any meaningful names. You can set the file to your
Read more
How to install sshfs on CentOS 7?
Posted onsshfs is a nice tool. But it seems there is no support to it in a newly installed CentOS 7 Linux system: Not installed by default: # sshfs -bash: sshfs: command not found Seems not available from the repositories # yum install sshfs -y Loaded plugins: fastestmirror Repodata is over 2 weeks old. Install yum-cron?
Read more
In Node.js, how to import functions from another JavaScript file?
Posted onIn Node.js, how to import functions from another JavaScript file like source common.sh in Bash? In the .js file to be imported such as ‘common.js’, organize functions to be exported like module.exports = { func1: function () { // func1 impl }, func2: function () { // func2 impl } }; In the other .js
Read more
How to process a file line by line in Go?
Posted onIn the Go programming language, How to process a file line by line? An equivalent piece of Bash code could be while read line ; do echo $line done < ./input.txt You can make use of the bufio package’s Scan(). // open the file filepath f := os.Open(filepath) // create a scanner fs := bufio.NewScanner(f)
Read more
How to get the full path and directory of a Makefile itself?
Posted onHow to get the full path and directory of a Makefile itself like finding Bash script’s own path? This 2 lines in Makefile get the full path and dir of the Makefile itself: mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) mkfile_dir := $(dir $(mkfile_path)) The piece of code gets you Makefile, finds its absolute path and the
Read more
How to grep a tab on Linux?
Posted onHow to grep a tab on Linux? Including ‘t’ seems not working. grep can grep tabs. The problem is likely the tab is not passed to grep. If you are using Bash, you can use ANSI-C quoting to pass the “tab” to grep: $ grep $’t’ file.txt Or, use Perl-style regex (only for GNU grep)
Read more
How to check whether a function has been defined in Bash?
Posted onI would like to know whether a function, say f1(), has already been defined in Bash. How should I do it? You can use the type -t to get a string of the type of the name: If the -t option is used, type prints a string which is one of alias, keyword, function, builtin,
Read more
How to disable an option with a bash script?
Posted onIt is possible to set an option inside a Bash script such as set -o errexit. But how to do the reverse thing, that is, to unset an option? Change – to + to do the reverse thing of unseting an option in bash. For example, to unset the errexit option set by set -o
Read more
How to test whether PATH has a specific directory in it in Bash on Linux?
Posted onHow to test whether PATH has a specific directory in it in Bash on Linux? For example, how to test whether /usr/local/mytool/bin is in $PATH? Directories in the $PATH variable are separated by :. So you can use a regular expression to test whether the $PATH contains a directory. The code for your specific example
Read more
How to test a file or directory exists in Python?
Posted onIn Bash, the [ -f ] and [ -d ] tests can test whether a file or a directory exist. What are the corresponding python method for these tests? For -f: **os.path.isfile(path)** Return True if path is an existing regular file. This follows symbolic links. For -d: **os.path.isdir(path)** Return True if path is an existing
Read more
How to get another user’s PATH in Bash on Linux?
Posted onHow to get the $PATH environment variables of another user in Bash on Linux? The current user running the Bash script is root. If the scripts is run by root, this piece of code will store the PATH from the environment of the user user1: user1path=$(su – $user1 -c env | grep ^PATH= | cut
Read more
How to get the script’s own path in sourced Bash script?
Posted onIn normal Bash script, $0 is the path to the script. However, when a script is sourced, such as . a.sh a.sh’s $0 does not give a.sh. How to get the a.sh inside a.sh? Short answer: use ${BASH_SOURCE[0]}. You can check this post for details and explanations: How to Get Bash Script’s Own Path.