1. 程式人生 > 其它 >linux指令碼檔案在任何路徑下輸入檔名可直接執行

linux指令碼檔案在任何路徑下輸入檔名可直接執行

技術標籤:linux系統編輯器

linux指令碼檔案在任何路徑下輸入檔名可直接執行

1編寫指令碼(或者以及寫好的指令碼)

編寫一個格式化記憶體卡的指令碼formatSD.sh

#!/bin/bash

### Parameter calibration
if [ $# -ne 1 ]; then
    echo "Usage: $0 <dev>"
    echo "    eg: $0 /dev/sdb"
echo "" exit 1; fi ### Variable definition SD_DEVICE=$1 SD_PARTITION1=$SD_DEVICE"1" ### Umount the sd device umount $SD_PARTITION1 if [ $? -ne 0 ]; then echo -e "\033[31m[ERROR] step1: umount sd device failed!\033[0m" else echo -e "\033[44;37m[OK] step1: umount sd device successful!\033[0m"
echo -e "\033[47;30m[OK] step1: umount sd device successful!\033[0m" fi ### Delete old partitions fdisk $SD_DEVICE << EOF d 2 d 1 w EOF if [ $? -ne 0 ]; then echo -e "\033[31m[ERROR] step2: Delete old partitions failed!\033[0m" exit 1 else echo -e "\033[44;37m[OK] step2: Delete old partitions successful!\033[0m"
echo -e "\033[47;30m[OK] step2: Delete old partitions successful!\033[0m" fi sleep 1 ### Create partition fdisk $SD_DEVICE << EOF n p 1 w EOF if [ $? -ne 0 ]; then echo -e "\033[31m[ERROR] step3: Create 1 partition failed!\033[0m" exit 1 else echo -e "\033[44;37m[OK] step3: Create 1 partition successful!\033[0m" echo -e "\033[47;30m[OK] step3: Create 1 partition successful!\033[0m" fi sleep 1 mkfs.vfat $SD_PARTITION1 -F 32 -n "Upan"

2把指令碼放入環境變數

如果我的指令碼路徑在 /home/noName/Documents/

2.1 開啟profile

gedit /etc/profile

2.2 把下面這句加入到檔案後面

export PATH=$PATH:/home/noName/Documents/

2.3同步資源

source /etc/profile

3執行指令碼

bash formatSD.sh /dev/sdb