How to debug the media print view set by @media print {} in CSS of Web pages in Firefox? In firefox, after opening the Web page, First, hit “Shift + F2” to open the “Developer Toolbar” at the bottom. Second, in the “Developer Toobar”, input media emulate print and Firefox will show the print view
Read more
Tag: Programming
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
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 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
Maximum number of mmap()’ed ranges and how to set it on Linux?
Posted onWhat’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 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 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).
Where is the source code for the free command on Linux?
Posted onWhere can I find the source code for the free command on Linux? The source code for the free commands (and many more, like kill, ps, top, sysctl) can be found in procps-ng: https://gitlab.com/procps-ng/procps procps is a set of command line and full-screen utilities that provide information out of the pseudo-filesystem most commonly located at
Read more
How to pass results from the opened window to the openning window in JavaScript?
Posted onFrom the opening window by JavaScript window.open(), we can pass info to the opened by hash. But how to pass results back from the opened window to the openning window in JavaScript? Assume in the opener window, a JavaScript variable is defined like var exchangeVar = ”; In the opened window, you can update the
Read more
How to capture the close event of an opened window by window.open() in JavaScript?
Posted onHow to capture the close event of an opened window by window.open() in JavaScript? This JavaScript code works well for me: var newwindow = window.open(“/newwindow.html”); newwindow.onbeforeunload = function () { // processing event here alert(“new window closed”); } Note that after the callback function is executed, the newwindow is closed.
Auto Pressing Multiple Keys Together in Linux
Posted onThis post introduces a key presser on Linux with xvkb. But how to simulate pressing multiple keys together on Linux? That is, I would like to have multiple keys such as “A” “B” “C” “D” pressed together and being hold. How to simulate this on Linux with programs/commands? You can make use of xdotool (xdotool:
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 get file size in Python?
Posted onHow to get a file’s size in Python from the full path of a file? You can use this piece of Python code: os.path.getsize(file_path) It returns the size in bytes. It raises os.error if the file does not exist or is inaccessible. Manual of os.path.getsize(): https://docs.python.org/2/library/os.path.html#os.path.getsize
How to iterate all files and directories under a directory in Python?
Posted onHow to iterate all files and directories under a directory in Python? For example, I would like to iterate each file and directory under /mnt/data/ /mnt/data/ |– file.txt `– great That is: /mnt/data/files.txt /mnt/data/great You can use the os.walk to do this: root, dirs, files = os.walk(path).next() root will be the path.dirs will contain the
Read more
How to get the file extension from a filename in Python?
Posted onHow to get the file extension from a filename in Python? For example, I would like to get “.txt” from “file.txt”. The Python code: >>> name, ext = os.path.splitext(‘file.txt’) >>> name ‘file’ >>> ext ‘.txt’ Manual of os.path.splitext: https://docs.python.org/2/library/os.path.html#os.path.splitext
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 get a FILE pointer from a file descriptor and how to get a file descriptor from a FILE pointer in C on Linux?
Posted onI would like to open files by open() directly. But how to get a FILE pointer from a file descriptor in C on Linux? And how to do the reverse direction: get a file descriptor from a FILE pointer? Get a FILE pointer from a file descriptor (e.g. fd) in C on Linux: FILE *file
Read more