Skip to content

Instantly share code, notes, and snippets.

View quwubin's full-sized avatar

Wubin Qu quwubin

View GitHub Profile
@mattes
mattes / check.go
Last active June 12, 2024 19:31
Check if file or directory exists in Golang
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
// path/to/whatever does not exist
}
if _, err := os.Stat("/path/to/whatever"); !os.IsNotExist(err) {
// path/to/whatever exists
}
@quwubin
quwubin / get_cpu_usage
Created March 25, 2014 08:17
Get CPU usage
#!/usr/bin/bash
# Give all CPUs usage percent
ps fuxw | awk '{ if ($3 ~ /^[0-9]/) {SUM +=$3}} END {print SUM"%"}'
@quwubin
quwubin / remove_blank_line.rb
Created June 8, 2012 01:31
Ruby: Remove blank lines and leading ^M characters
#!/usr/bin/env ruby
#
# Wubin Qu <quwubin@gmail.com>
#
if ARGV.size != 1
$stderr.puts "
Remove blank lines and leading ^M characters
Usage:
@JosephPecoraro
JosephPecoraro / shell-execution.rb
Last active September 10, 2023 10:12
Shell Execution in Ruby
# Ways to execute a shell script in Ruby
# Example Script - Joseph Pecoraro
cmd = "echo 'hi'" # Sample string that can be used
# 1. Kernel#` - commonly called backticks - `cmd`
# This is like many other languages, including bash, PHP, and Perl
# Synchronous (blocking)
# Returns the output of the shell command
# Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111