How to do “contains string” test in Bash?

Posted on

How 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

‘dd’ command cannot support calculation for its parameters

Posted on

$ dd if=/dev/zero of=./4Ktest100M bs=4KB count=25000*9 dd: invalid number `25000*9′ I think ‘dd’ should support calculation for its parameters like ‘bs’, ‘count’ and so on. You can get the effect by using some other tools/commands, like dd if=/dev/zero of=./4Ktest100M bs=4KB count=$((25000*9)) or dd if=/dev/zero of=./4Ktest100M bs=4KB count=$(bc <<< 25000*9) I think it makes sense. Thank
Read more

How to attach and mount Xen DomU’s disk partitions on Linux without Xen?

Posted on

How to attach and mount Xen DomU’s disk partitions on Linux without Xen? You can use kpartx to create devices from the VM disk image (say, ./vmdisk0). To activate all the partitions in a raw VM disk image: # kpartx -av ./vmdisk0 This will output lines such as: add map loop1p1 (253:8): 0 497664 linear
Read more

How to get a script’s directory reliably in Bash on Linux?

Posted on

How 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

Why does ; after & lead to unexpected token error in bash on Linux?

Posted on

The 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 manually kill HDFS DataNodes?

Posted on

stop-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

Firefox: how to sync bookmarks saved on iOS devices to Firefox on PC?

Posted on

Firefox on iOS can view bookmarks from PC. But how to sync the bookmarks made on iOS to PC? It seems the bookmarks made in Firefox on iPhone is local only. No, you can’t yet. Bookmarks saved on your iOS devices will not sync to your PC at current (by Nov. 25, 2015) version of
Read more

Maximum number of mmap()’ed ranges and how to set it on Linux?

Posted on

What’s the maximum number of mmap()‘ed ranges that a process can makes and how to set the limits on Linux? I have a program that mmap()s and mprotect()s lots ranges. After allocating many ranges, mprotect() starts to fail with ENOMEM error number. From the man page, ENOMEM means 2 possible problems: ENOMEM Internal kernel structures
Read more

How to configure ifcfg-eth0 network scripts on Fedora/RHEL Linux?

Posted on

How 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).