Sunday, September 21, 2014

bash


Bash Round
chang@ACPNEW:~/tmp$ echo "scale = 5; 5+50*3/20 + (19*2)/7" | bc -l | xargs printf "%2.3f\n"
17.929

Avg
chang@ACPNEW:~/tmp$ awk '{s+=$1}END{print "ave:",s/NR}' RS=" "  avg.txt
ave: 54.646

for loop

for i in {1..50}
do
  echo $i
done


chang@ACPNEW:~/tmp$ cat b.txt | tr -s " "


tab seperated
cut -d$'\t' -f2-

chang@ACPNEW:~/tmp$ cut -d$'\t' -f2- b.txt




Tuesday, August 19, 2014

wordpress menu get erased

My wordpress menu get erased randomly. And I cannot replicate this issue.

I duplicated my whole site to a testing site, and dump the DB.
One day, I found my menu is gone, and I check the database,

mysql> select post_title, post_name, post_type from wp_posts where post_type='nav_menu_item' ;

It return 0 menu.

I disabled the plugin  Stealth Publish and fixed the issue.

It took me 3 weeks find the solution.

Tuesday, May 13, 2014

rails paperclip disable validation

1, Disable from global config file: config/application.rb

require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

2, Disable from controller
class Logserver < ActiveRecord::Base
  has_attached_file :serverkey, :path => "public/keys/:id/:filename", :url => "/keys/:id/:basename.:extension"
  #validates_attachment_content_type :serverkey, :content_type => /\Atext.*\Z/
  do_not_validate_attachment_file_type :serverkey
 end

Wednesday, April 30, 2014

install awscli

apt-get install python-pip
pip install awscli

aws configure

aws ec2 describe-instances

aws ec2 run-instances --image-id ami-3495735c --count 1 --instance-type t1.micro

Friday, February 28, 2014

Github/Markdown Gem usage

Under root:
1, install github markdown gem
gem install github-markdown

2, Add markdown gem into Gem file
 root@ubuntuChef:/opt/ruby/user# vim Gemfile
gem 'github-markdown'

3, may need do:
bundle install

4, Define your markdown in your controller
I put MD file under app/assets/markdown folder

ang@ubuntuChef:/opt/ruby/user$ vim app/controllers/myusers_controller.rb

  def markdown
    #@myusers = Myuser.all
    require 'github/markdown'
    #require 'rdiscount'

    mdfile = File.join(Rails.root, 'app', 'assets', 'markdown', 'test.md')
    mymd = File.open(mdfile, 'rb') { |file| file.read }
    @md = GitHub::Markdown.render(mymd)
    #@md =  RDiscount.new(mymd, :smart, :filter_html).to_html
  end

5, update the view  - DON'T forget RAW

root@ubuntuChef:/opt/ruby/user# vim app/views/myusers/markdown.html.erb
<h1>Markdown</h1>

<%= raw(@md) %>