How to test whether a user account, say linuxuser, already exist on Linux? You may make use of id which tries to get user IDs. The Bash code snippet is as follows. user=hello id -u $user >/dev/null 2>&1 if [ “$?” == “0” ]; then echo “user $user already exist.” else echo “user $user doesn’t
Read more
Tag: Bash
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
Linux boots failed with “sulogin: can not open password database” while the /etc/passwd and /etc/shadow files look fine
Posted onLinux boots failed with “sulogin: can not open password database” while the /etc/passwd and /etc/shadow files look fine. How to fix this? The OS is Fedora 22. You may try this trick Step 1: boot Linux with rw init=/bin/bash following this tutorial. Step 2: after Linux booted, disable SELinux following this tutorial. Reboot. If it
Read more
How to sort lines by length in Linux?
Posted onI have a text file with many lines of text. In Linux, how to sort the lines by their length? You can make use of awk together with the sort program like awk ‘{ print length(), $0 | “sort -n” }’ /path/to/text/file Here, we use awk to calculate the text length and sort -n to
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
Why does ; after & lead to unexpected token error in bash on Linux?
Posted onThe command using ‘;’ after ‘&’ like ssh host1 hostname &; ssh host2 hostname & Leads to error like bash: syntax error near unexpected token `;’ Why does ; after & lead to unexpected token error in bash on Linux? And what’s the solution? The fix The quick fix is to change your command to
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 watch a directory size in Linux Shell
Posted onHow (only) to show the size of the directory. LS command may show more trivial infos I don’t need. DU command can do it. DU is “du – estimate file space usage”. Users could use ‘–max-depth=N’ parameter to print the level of information. One example is as follows. $ du -h –max-depth=0 hummer-svn 11G hummer-svn
How to keep master thesis safety and availability on Windows
Posted onWhen you write your master thesis (in Chinese) on windows, you may have following worries. a, Be afraid your master thesis is lost (or can not be accessed) when hard disk/udisk is broken (or something other viruses). b, Keep master thesis availability (7×24) and safety. c, Do not want anyone else to access it before
Read more
How to merge sam files on Linux?
Posted onThe samtools merge can merge bam files while it can not work for sam files. How to merge sam files on Linux? According to the sam format specification, header lines start with @, while alignment lines do not. So you can use grep to merge sam files as follows efficiently. Assume the header is from
Read more
How to cut by multiple spaces in Linux?
Posted onHow to cut by multiple spaces in Linux? For example, if I get a string like a b c d where the spaces among the fields are not decided. How to get the four fields a, b, c and d out? Here, we take getting ‘b’ (the 2nd field) as the example. You can first
Read more
How to detect whether Linux runs in UEFI or BIOS mode inside the Linux?
Posted onHow to detect whether Linux runs in UEFI or BIOS mode inside the Linux itself without needed to boot the the management console of the mother board? You can detect whether Linux runs in EFI mode by checking whether /sys/firmware/efi exist. In bash, you can test by [ -d /sys/firmware/efi/ ] This technique is used
Read more
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 force umount a NFS directory on Linux?
Posted onThe NFS server is down. It is reported “Stale NFS file handle”. I tried ‘umount’ and ‘umount -f’. But neither succeeded. # umount /mnt/store umount.nfs: /mnt/store: Stale NFS file handle # umount -f /mnt/store umount.nfs: /mnt/store: Stale NFS file handle How to force umounting the NFS without rebooting the Linux server? I usually have to
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