shell脚本获取当前目录的绝对路径

if [ "/" != ${0:0:1} ];then
        fileCurrent=$(pwd)/$0
else
        fileCurrent=$0
fi
#有些情况会有这样的路径/root/ares/./test.sh
fileCurrent=${fileCurrent/\/\.\//}
dirCurrent=${fileCurrent/\/\.}
dirCurrent=${dirCurrent%/*}

脚本可以以任何方式执行,例如:
/root/ares/test.sh
cd /root/ares && ./test.sh
cd /root && ares/test.sh

如果脚本是一个软链的话路径是软链的位置。

另外一种方式:

#!/bin/bash
dir=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)

 

js加密(编码)email等

<script language="javascript" type="text/javascript"> 
    function getEmail(email) 
    { 
        var r = ""; 
        for (i=0;i<email.length;i++) 
        { 
            r += "&#" + email.charCodeAt(i); 
        } 
        return r; 
    } 
     
     function setClipboard(id) 
    { 
        var tb = document.getElementById(id); 
        var copytext = tb.value; 
        if (copytext!="") 
        { 
            if (window.clipboardData) 
            { 
                window.clipboardData.setData("Text", copytext); 
                alert("已复制到剪贴板。"); 
            } 
            else if (window.netscape) 
            { 
                setClipboardNetscape(copytext); 
                alert("已复制到剪贴板。"); 
            } 
        } 
        return false; 
    } 
     
    function setClipboardNetscape(text){ 
        var url = [ 
            'data:text/html;charset=utf-8;base64,PGJvZHk+PC9ib2', 
            'R5PjxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0Ij4KKGZ1', 
            'bmN0aW9uKGVuY29kZWQpe3ZhciBzd2ZfZGF0YSA9IFsKICdkYX', 
            'RhOmFwcGxpY2F0aW9uL3gtc2hvY2t3YXZlLWZsYXNoO2Jhc2U2', 
            'NCxRMWRUQjJ3JywKICdBQUFCNG5EUGdZbGpBd01qSTRNejAlMk', 
            'YlMkY5JTJGZTJaZkJnYUdhV3dNRE1uNUthJywKICdrTU10TjRH', 
            'ZGdaZ1NJTXdaWEZKYW01UUFFJTJCQm9iaTFCTG5uTXlDcFB6RW', 
            '9oU0dJJywKICdQRnAlMkZBeHNEREJRa3BGWkRGUUZGQ2d1eVM4', 
            'QXlqSTRBRVVCaXkwVndBJTNEJTNEJwpdLmpvaW4oIiIpOwpkb2', 
            'N1bWVudC5ib2R5LmlubmVySFRNTCA9IFsKICc8ZW1iZWQgc3Jj', 
            'PSInLHN3Zl9kYXRhLCciICcsCiAnRmxhc2hWYXJzPSJjb2RlPS', 
            'csZW5jb2RlZCwnIj4nLAogJzwvZW1iZWQ+JwpdLmpvaW4oIiIp', 
            'Owp9KSgi', 
            base64encode( encodeURIComponent(text) + '")</'+'script>') 
        ].join(""); 
        var tmp = document.createElement("div"); 
        tmp.innerHTML = [ 
             '<iframe src="',url,'"' 
            ,' width="0" height="0">' 
            ,'</iframe>' 
        ].join(""); 
        with(tmp.style){ 
            position ="absolute"; 
            left = "-10px"; 
            top  = "-10px"; 
            visibility = "hidden"; 
        }; 
        document.body.appendChild(tmp); 
        setTimeout(function(){document.body.removeChild(tmp)},1000); 
        function base64encode(str){ 
            var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""); 
            var c1, c2, c3; 
            var buf = []; 
            var len = str.length; 
            var i = 0; 
            while(i < len){ 
                c1 = str.charCodeAt(i) & 0xff; 
                c2 = str.charCodeAt(i+1); 
                c3 = str.charCodeAt(i+2); 
                buf.push(Chars[(c1 >> 2)]); 
                if(i+1 == len){ 
                    buf.push(Chars[(c1 & 0x3) << 4],"=="); 
                    break; 
                } 
                buf.push(Chars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]); 
                if(i+2 == len){ 
                    buf.push(Chars[(c2 & 0xF) << 2],"="); 
                    break; 
                } 
                buf.push( 
                    Chars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)], 
                    Chars[(c3 & 0x3F)] 
                ); 
                i+=3; 
            } 
            return buf.join("") 
        } 
    } 
</script>

<p><strong>Email地址加密</strong></p>
<p> Email:
  <input name="email" type="text" id="email" size="50" />
  <input type="button" name="Submit" value=" 加 密 " onclick="document.getElementById('export').value=getEmail(document.getElementById('email').value)"/>
  
  <input type="button" name="Submit2" value=" 验 证 " onclick="document.getElementById('preview').innerHTML=getEmail(document.getElementById('email').value)"/>
  <span id="preview" style="color:#FF0000"></span > </p>
<p> 结 果:
  <input name="export" type="text" id="export" size="76" onclick="setClipboard(this.id)" onmouseover="javascript:this.select();" title="单击此处复制内容"/>
</p>

 

js水平和垂直居中函数

function centerBox(obj){
	var $obj;
	if(obj instanceof jQuery){
		$obj=obj;
	}else{
		$obj=$(obj);
	}
	$obj.show();
	center($obj);
	$(window).scroll(function(){
		center($obj);
	});
	$(window).resize(function(){
		center($obj);
	});
}

function center($obj){
	var windowWidth = document.documentElement.clientWidth; 
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $obj.height();
	var popupWidth = $obj.width();
	$obj.css({   
		"position": "absolute",
		"top": (windowHeight-popupHeight)/2+$(document).scrollTop(),   
		"left": (windowWidth-popupWidth)/2   
	});  
}

 

PHP动态输出JS

$url = $this->getConfig ()->url ();
$content = "var url={ base:'{$url->base}',css:'{$url->css}',js:'{$url->js}',img:'{$url->img}',current:window.location};";
$expire = 604800;
header ( 'Content-type: application/x-javascript' );
header ( 'Cache-Control: max-age=' . $expire );
header ( 'Accept-Ranges: bytes' );
header ( 'Content-Length: ' . strlen ( $content ) );
echo $content;