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: Script
Any good Go REPL implementation / tool?
Posted onGo lang is fast to compile and can be used as a script by go run prog.go. However, a REPL is more convenient for testing/trying some small pieces of code. Any good Go REPL implementation / tool? Try gore https://github.com/motemen/gore Get gore $ go get -u github.com/motemen/gore An example $ gore gore version 0.2.6 :help
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 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 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 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.
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
How to do “contains string” test in Bash?
Posted onHow to test whether a string $str contains another string $needle in Bash? You can use this piece of Bash script: [[ “$str” == *”$needle”* ]] A usage example: $ str=”abcde hello” $ needle1=”deh” $ needle2=”de hello” $ [[ “$str” == *”$needle1″* ]] && echo “matched” $ [[ “$str” == *”$needle2″* ]] && echo “matched”
Read more
How to remove trailing / in path in Bash?
Posted onI am writing a Bash script accepting input of a path from the user. I want to make the path have no trailing ‘/’, such as ‘/path/to/dir’ instead of ‘/path/to/dir/’. However, as the input is from the user, the input could be ‘/path/to/dir’. My script is intended to handle this. The question is: how to
Read more
How to get a script’s directory reliably in Bash on Linux?
Posted onHow to get a script’s directory reliably in Bash on Linux? For example, to get the directory of the executing script $0. dirname can give you the directory name from the absolute path. You can get the absolute path of the script by readlink -f to handle symbolic links (consider a symbolic link ./run.sh linked
Read more
How to use the xargs argument twice in the command on Linux?
Posted on`xargs` passes the argument once to the utility command specified. For example, xargs cat will cat every line passed to xargs. But how to use the xargs argument twice in the command on Linux? For example, to rename file to file.bak where file is from the stdin. One solution is to write a small script like
Read more
How to manually kill HDFS DataNodes?
Posted onstop-dfs.sh report that there are no datanodes running on some nodes like hdfs-node-000208: no datanode to stop However, there are DataNode process running there. How to clean these processes on many (100s) of nodes? You may use this piece of bash script: for i in `cat hadoop/etc/hadoop/slaves`; do echo $i; ssh $i ‘jps | grep
Read more
How to configure ifcfg-eth0 network scripts on Fedora/RHEL Linux?
Posted onHow to configure the ifcfg-* network script files under /etc/sysconfig/network-script/ on Fedora/RHEL Linux? Following is a very typical ifcfg-eth0 file setting static IP/gateway/network mask: DEVICE=eth0 BOOTPROTO=none ONBOOT=yes NETMASK=255.255.0.0 IPADDR=192.168.1.151 GATEWAY=192.168.1.10 USERCTL=no More options are supported, here you can find a reference: Interface Configuration Files (a easier to read PDF from F15 doc).
How to test whether the git repository is dirty?
Posted onHow to test whether the git repository is dirty? git status can show this. But how to diagrammatically detect this in bash? This piece of bash code works like a charm for me: [[ $(git diff –shortstat 2> /dev/null | tail -n1) != “” ]] You can use it to build your script. For example,
Read more
How to enable SSH service on Fedora Linux?
Posted onHow to enable SSH service on Fedora Linux? By default, it seems ssh is not enabled. Fedora may not have sshd service installed/enabled by default. You will need to install and configure it by yourself. The following instructions is for Fedora 22 as an example. First, install the sshd server by # dnf install openssh-server
Read more
How to merge 2 .a libraries to one .a library on Linux?
Posted onWe have 2 static .a libraries and we would like to merge them into a single one for easier usage. How to merge 2 .a libraries to one .a library on Linux? With GNU ar, you can specify the single command-line option -M and control it with a script supplied via standard input, like the
Read more
How to make Fedora Linux not clean some files in /tmp/?
Posted onOn my Fedora 20, I find that the system automatically clean up file under /tmp/. This is convenient. However, it cause some problems for some programs. For example, HDFS puts its DataNode pid file under /tmp/ by default like hadoop-hadoop-datanode.pid. After it is cleaned up, the hadoop-daemon.sh script will consider there is no DataNode running.
Read more
Printing “powernow-k8 transition frequency failed” after install XEN
Posted onAfter I installed XEN hypervisor, the tty0 (CTRL+ALT+F1) prints “powernow-k8 transition frequency failed” to STDOUT continously, how to solve it? sudo rmmod powernow_k8 From https://lists.debian.org/debian-user/2013/01/msg00754.html How to make the settings persistent (take effect after reboot)? 1, Create script file ‘/etc/init.d/stop_pwoerk8.sh’ as follows. harry2vera@node1:/etc/init.d$ cat stop_pwoerk8.sh #! /bin/sh rmmod powernow_k8 2, Add the full PATH of
Read more