In Golang, the fmt.Println() is convenient to print to STDOUT. But how to print string to STDERR? In Go os package, “Stdin, Stdout, and Stderr are open Files pointing to the standard input, standard output, and standard error file descriptors.” So, you can use the WriteString function on File on os.Stderr to print string to
Read more
Tag: Programming
Converting Error to String in Go Lang
Posted onIn Golang, fmt.Println(err) can print out the error err. But how to convert an error to a string explicitely? In Go, the error type is an interface type that has an interface type error interface { Error() string } An error variable represents any value that can describe itself as a string. Ref: https://golang.org/pkg/builtin/#error So,
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 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 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 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 get the CPU temperatur in command linux on Linux?
Posted onMost modern servers or computers have sensors to detect the temperature of various components. On Linux, how to get the CPU core temperatur in command linux? First, make sure the package “lm-sensors” is installed on your Linux and the command sensors works for you. Then, you can use this piece of script to get the
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 file extension in JavaScript?
Posted onHow to get file extension from a file name in JavaScript? For examples, For “file.txt”, I want to get “txt”. For “file2.multi.ext.fileext”, I want to get “fileext”. This JavaScript function works well for me. function getExt(filename) { var idx = filename.lastIndexOf(‘.’); // handle cases like, .htaccess, filename return (idx < 1) ? “” : filename.substr(idx
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.
How to move cursor to a specific byte in Vim?
Posted onIn Vim, ’20G’ moves the cursor to line 20 in command mode. But how to move the cursor to a specific byte in Vim? In normal mode, 20go will move the cursor to byte 20. Or in command line, :goto 20 or :go 20 From vimdoc: :[range]go[to] [count] [count]go Go to {count} byte in the
Read more
How to get a user’s home directory from the username in a variables in Bash?
Posted onIn Bash, ~username gives the username user’s home. However, this does not work for situations where username is in a variable such as ~$user How to get the home directory from the user name in a variable in Bash? You can use this Bash script snippet: userhome=$(eval echo ~$user) Here, $user get evaluated to its
Read more
In Vim, how to search and replace in visual selection?
Posted onSearch and replace in the whole text in Vim can be done by :%s/from/to/gc. But how to search and replace in visual selection only? The %V atom in Vim restricts a pattern so that it matches only inside the visual selection. So, your command for searching and replacing only in the visual selection can be
Read more
How to get the top committers in git?
Posted onHow to get the top committers in git? Like in github: But plain text is fine showing number of commits and author names/emails sorted by the number of commits. You may use this command: git log | grep Author | sort | uniq -c | sort -n -r Here, count the number of commits by
Read more
How to manually set Vim’s filetype?
Posted onVim can detects the file types from quite well. But under certain situations, such as creating a new file, Vim does not automatically set the file type. How to manually set it? In side Vim, to set a filetype, set value to the filetype variable. For example, to set the filetype to php: :set filetype=php
How to get the latest git commit SHA-1 in a repository?
Posted onHow to get the latest git commit SHA-1 id in a repository? And how to get the first 8 digits of the SHA-1? Instead of the method introduced here, you may use $ git rev-parse HEAD to get the commit SHA-1 hash ID. If you want to get the first 8 digits only, use $
Read more