網頁

2018年3月11日 星期日

Ubuntu 忘記密碼的重置

有時候太多密碼是有可能忘記的,前幾天要使用一台安裝 Ubuntu 的筆電 ,由於太久沒有使用了竟然忘了登入密碼,如果Ubuntu忘記密碼無法登入怎麼辦?  下面是我的方法可以參考。

1. 首先用 LiveCD 開機,選擇 Try Ubuntu MATE

 2. 開啟終端機,輸入 lsblk,看看目前磁碟的分割磁區有哪些,在這例子主要分割是 sda1


3. 接著輸入 sudo mount /dev/sda1 /mnt,將 sda1 掛載到 /mnt 目錄
然後輸入 sudo chroot /mnt,這時提示符號會成為 #
然後輸入 passed root ,更改 root 密碼
如果使用者密碼也需要更改就 passwd user-name







4. 輸入 sudo umount /mnt ,將 /mnt 目錄卸載,重新開機即可。



2017年12月22日 星期五

UNIX入門課程1061_ex10

 http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html

作業10: awk programming, associative array
如下例所示, 計算每個人的總和.
$ cat IN-7
# sample input file of exercise 7
                    # this is an blank line.
1234 Alice          # Alice wins 1234
999  Bob            # Bob wins 999
Alice 200           # Alice spends 200
Bob 100 Alice       # Bob loses 100 to Alice
4000 Bob 250 Carol  # Bod wins 4000 and loses 250 to Carol
3000 Carol 300      # Carol wins 3000 and spends 300

$ ./ex10.awk IN-7 Carol has 2950 Bob has 4649 Alice has 1134
$

解答:

#!/usr/bin/awk -f
BEGIN {
}
{
  sub(/#.*/,"") # change #.*  to ""
  for (i=1; i<=NF; i++) {
    if ($i ~ /^[0-9]/) {   #-----$1 is number
      money=$i
      if (i==2 || i==3) {  #-----money is spends or loses
        name=$(i-1)
        array[name]=array[name]-money
      }
    } else {               #-----$1 is name
      name=$i
      money=$(i-1)
      array[name]=array[name]+money
    }
  } #for END
}
 
END {
  for (name in array)
    printf("%s has %s\n", name, array[name])
}

執行結果:



2017年12月15日 星期五

UNIX入門課程1061_ex9

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html
作業九: listing a directory recursively
•建立一個 shell 執行檔, 名為 showdir, 可印出 類似用 tree -F 所得的結果.
若檔案為一目錄, 則在檔名後加 '/'
若檔案為一 symbolic link, 用箭頭顯示. 如上圖所示.
若目錄無法讀取內容, 則略過其以下的內容, 即只顯示目錄名字.
請勿用 find, du 或其他指令先得到一個 tree.


$./showdir /var/tmp/exer9-test-dir
exer9-test-dir/
|---city/
|   |---lapula --> ../sky/temple/utopia
|   `---rome
|---dogs/
`---sky/
    |---birds
    |---mountains
    `---temple/
        |---asking
        `---utopia

$ tree -F /var/tmp/exer9-test-dir
/var/tmp/exer9-test-dir
|-- city/
|   |-- lapula -> ../sky/temple/utopia
|   `-- rome
|-- dogs/
`-- sky/
    |-- birds
    |-- mountains
    `-- temple/
        |-- asking
        `-- utopia
     
4 directories, 6 files
$


解答:
#!/bin/bash
olddir=$PWD
dirdepth=0
function is_last_file {
  local flag
  let flag=${ary[dirdepth]}-1
    if [ $flag -eq 0 ]; then #last file
      ch="\\"  #as \---
    else
      ch="|"   #as |---
    fi
}
function print_file {
  is_last_file #check this file is last?
  case "$1" in
      -D) printf "$ch---$2/\n"
          ;; #file is directory
      -L) printf "$ch---$file --> "
          ls -l $file | awk '{ print $11 }'
          ;; #file is link
      -F) printf "$ch---$file\n" #normal file
          ;; #file is normal file
  esac
  let ary[dirdepth]=ary[dirdepth]-1
}
function check_file_type {
  if [ $dirdepth -gt 0 ]; then  
          for ((i=0; i<=dirdepth-1; i++)); do
            if [ ${ary[i]} -ne 0 ]; then
              printf "|   " #print | & 3 space
            else
              printf "    " #print 4 space
            fi
          done # for END
  fi
  if [ -d "$file" ]; then 
      print_file -D $file #directory  
  else          
      if [ -L $file ]; then
          print_file -L $file #symbolic link
      else 
          print_file -F $file #normal file
      fi
  fi
}
function listfiles {
  cd "$1";
    f_count=`ls -l | wc -l`  #calc total files 
    let f_count=f_count-1
    ary[dirdepth]=$f_count

  if [ $f_count -ne 0 ]; then # if dir is empty then exit
    for file in *; do
      check_file_type $file #call sub
      if [ -d "$file" ]; then
          let dirdepth=$dirdepth+1
          listfiles "$file"
          cd ..;
      fi
    done #for END
  fi
  let dirdepth=$dirdepth-1
}
#-----Main Program 2017/12/06 update-----
#d=$1
#echo "${d##*/}/"
echo "$(basename "$1")/"
listfiles "$1"
 
執行結果:




2017年12月9日 星期六

UNIX入門課程1061_ex8

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html

作業八: hanoi tower
•建立一個 shell 執行檔, 名為 hanoi, 可印出 hanoi tower problem 中 disc 移動的順序

$ ./hanoi 2    # move 2 discs, from A to B by C
from A to C
from A to B
from C to B
$ ./hanoi 3    # move 3 discs, from A to B by C
from A to B
from A to C
from B to C
from A to B
from C to A
from C to B
from A to B
$

解答:

#!/bin/bash
function move_disc {
  if [ $1 -ne 0 ]; then
    move_disc $(($1-1)) $2 $4 $3
      echo from $2 "to" $3
    move_disc $(($1-1)) $4 $3 $2
  fi
}
#-----Main Program-----
disc=$1
if [ $disc -gt 0 ]; then
  move_disc $disc A B C
fi

執行結果:


 

2017年12月2日 星期六

UNIX入門課程1061_ex7

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html

作業七: range filter with sh
•建立一個 shell 執行檔, 名為 range, 其使用方法與上題類似, 但參數可能有二個, 可濾出上下限中間的數.
•從 stdin 讀入數字, 一行內的數可能超過一個
$ cat Numbers
6
7
8
9
10
11
12
13
14
129
37
8
119
$ cat Numbers | ./range 10 60
10
11
12
13
14
37
$ cat Numbers | ./range - 60
6
7
8
9
10
11
12
13
14
37
8
$ cat Numbers | ./range 200
$ cat Numbers | ./range 120
129
$ cat Numbers | ./range
6
7
8
9
10
11
12
13
14
129
37
8
119
$

解答:
#!/bin/bash
function Filter_Number() {
  local opt=$1
  local input_num=$2
  case "$opt" in 
    0)echo $input_num
      ;;
    1)if [ $input_num -ge $range_s ];then
        echo $input_num fi
      ;;
    2)if [ $range_s = "-" ]; then
        if [ $num -le $range_e ]; then
          echo $input_num
        fi 
      elif [ $input_num -ge $range_s -a $input_num -le $range_e ];then
        echo $input_num
      fi
      ;;
    esac
}

#-----main program-----
argc=$#
#echo "arg have:$argc"
case "$argc" in
  0);;
  1)range_s=$1
    ;;
  2)range_s=$1 range_e=$2
    ;;
esac
while read data
do
  #set $(echo $data)
  for num in $(echo $data)
    do
      #echo $num
      Filter_Number $argc $num
    done
done

執行結果:

 


2017年11月25日 星期六

UNIX入門課程1061_ex6

 http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html

作業六: low pass and high pass filter with sh
•建立二個 shell 執行檔, 名為 less-than, greater-than
•每個執行檔會用到第一個參數當作 bound
•從 stdin 每一行讀一個數字, 滿足條件則印出到 stdout,
$ cat numbers
19
23
101
55
34
$ cat numbers | ./less-than 60
19
23
55
34
$ cat numbers | ./less-than 60 | ./greater-than 30
55
34
$

解答:
#-----less-then-----
#!/bin/sh
while read number
do
  if [ $1 -gt $number ]; then
    echo $number
  fi
done

#-----greater-then-----
#!/bin/sh
while read number
do
  if [ $number -gt $1 ]; then
    echo $number
  fi
done





執行結果:

2017年10月17日 星期二

對於使用Linux的人來說終端機(terminal)是不可或缺的工具,今天介紹的 Guake Terminal 和一般的 terminal 最大的不同就是它會定在螢幕的上方,按 F12 就會顯示出來,非常方便。 guake-1.jpg
安裝方式非常簡單: sudo apt-get install guake sudo apt-get update 安裝後如果希望在登入後就啟動常駐,在LXDE桌面系統下可以執行:
sudo ln -s /usr/share/applications/guake.desktop /etc/xdg/autostart/ 

更改設定可以在視窗西岸的情況下按下滑鼠右鍵,選擇 Preferences :
guake-2.jpg

可以修改一些設定或是自訂快捷鍵: guake-3.jpg