6 月 252021
 

Source: 程式結束狀態: Windows 的 %ERRORLEVEL% 與 Linux 的 $?

不管是在 Linux 下撰寫 Bash 指令檔或是在 Windows 下撰寫批次檔,最近一次程式執行的回傳代碼是非常重要的撰寫技巧這可以協助我們在撰寫自動化指令檔時能夠更有效的處理程式執行失敗的狀況,便可進一步撰寫條件式處理各種執行失敗的狀況,但是在 Windows 與 Linux 各有什麼異同之處呢?讓我們繼續看下去。

Continue reading »
7 月 202018
 

Source: 10 Ways to Generate a Random Password from the Linux Command Line

One of the great things about Linux is that you can do the same thing hundreds of different ways—even something as simple as generating a random password can be accomplished with dozens of different commands. Here’s 10 ways you can do it.

Continue reading »

11 月 222016
 

Source: Test from shell script if remote TCP port is open

I'm looking for a quick and simple method for properly testing if a given TCP port is open on a remote server, from inside a Shell script.

I've managed to do it with the telnet command, and it works fine when the port is opened, but it doesn't seem to timeout when it's not and just hangs there...

Here's a sample:

l_TELNET=`echo "quit" | telnet $SERVER $PORT | grep "Escape character is"`
if [ "$?" -ne 0 ]; then
  echo "Connection to $SERVER on port $PORT failed"
  exit 1
else
  echo "Connection to $SERVER on port $PORT succeeded"
  exit 0
fi

I either need a better way, or a way to force telnet to timeout if it doesn't connect in under 8 seconds for example, and return something I can catch in Shell (return code, or string in stdout).

I know of the Perl method, which uses the IO::Socket::INET module and wrote a successful script that tests a port, but would rather like to avoid using Perl if possible.

Continue reading »

10 月 192015
 

Source: Bash Shell Check Whether a Directory is Empty or Not

How do I check whether a directory is empty or not under Linux / UNIX using a shell script? I would like to take some action if directory is empty on a Linux or Unix like system. How can I check from bash/ksh shell script if a directory contains files? How do I check whether a directory is empty or not?

Continue reading »

4 月 132014
 

原文(source): http://blog.longwin.com.tw/2012/11/bash-prompt-set-generator-2012/

Linux 使用 Bash shell 可以自訂 prompt 的格式(設定 Shell 的 PS1 變數), 先把自己現在和以前有做過的一些設定列出來~

  • PS1='\u@\h:\w\$ '
  • PS1='\[\u@\h:\e[32m\w\e[0m$ \]'
  • PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
  • PS1='${debian_chroot:+($debian_chroot)}\A/$? \[\033[01;32m\][\u@\h] \[\033[01;34m\]\w \$ \[\033[00m\]'

Continue reading »