Linux下shell指令碼程式設計

2021-06-21 21:12:59 字數 2148 閱讀 3509

shell程式設計小練習:

在linux作業系統中,如果插入乙個usb裝置,需要用mount掛載命令才能實現這個裝置的載入,下面寫乙個usb裝置掛載與檔案複製的shell程式,程式需求:

1、執行時,提示使用者輸入「y」或者「y」,確定是否掛載usb裝置,u盤檔案/dev/sdc1

if[$ans="y" -o $ans = "y"]

then mount -t vfat /dev/sdc1 /mnt/usb

2、確定是否複製檔案到/root

最好用$?判斷一下是否複製成功,$? -eq 0,表示複製成功

while[$ans="y" -o $ans = "y"]

do ls -lha /mnt/usb

echo "type the filename you want to copy"

read file

cp /mnt/usb/"$file" /root

3、確定是否複製檔案到usb裝置中

echo "do you want to copy files to usb(y/n)"

read ans

while[$ans="y" -o $ans = "y"]

do ls -lh /root

echo "type the filename you want to copy"

read file

cp /root/"$file" /mnt/usb

if[ $? -eq 0];then

echo "finished"

else

echo "error"

fi echo "any other files(y/n)"

read ans

done

完整的指令碼:

#!/bin/bash

#autousb

echo "welcome to usb"

echo "do you want load usb(y/n)"

read ans

if[$ans="y" -o $ans = "y"];

then mount -t vfat /dev/sdc1 /mnt/usb

echo "do you want to copy files to /root(y/n)?"

read ans

while[$ans="y" -o $ans = "y"]

do ls -lha /mnt/usb

echo "type the filename you want to copy"

read file

cp /mnt/usb/"$file" /root

if[ $? -eq 0];then

echo "finished"

else

echo "error"

fi echo "any other files(y/n)"

read ans

done

fi echo "do you want to copy files to usb(y/n)"

read ans

while[$ans="y" -o $ans = "y"]

do ls -lh /root

echo "type the filename you want to copy"

read file

cp /root/"$file" /mnt/usb

if[ $? -eq 0];then

echo "finished"

else

echo "error"

fi echo "any other files(y/n)"

read ans

done

echo "do you want to umount?(y/n)"

read ans

if[$ans="y" -o $ans = "y"];then

umount /mnt/usb

else

echo "umount error"

fi echo "goodbye!!"

Linux下Shell指令碼程式設計簡介

最近經常使用linux,感覺太頻繁地敲擊鍵盤有些累了,於是想到了shell指令碼。可以把太多的命令寫成乙個指令碼,這樣每次執行一遍sh檔案,就可以省去了敲擊鍵盤的時間,還可以保護鍵盤哦!於是在網上搜了一些有關linux下指令碼程式設計的內容。shell不僅僅是命令的收集,而且是一門非常棒的程式語言。...

linux下的shell指令碼程式設計

程式語言分為兩種,一種是整個 進行編譯然後進行執行 另一種就是shell這種通過直譯器一行一行轉換為 linux常用的一些命令此處不再贅述 直接進入主題 我們來寫第乙個程式 hello world bin bash 上面的 告訴了直譯器採用bash直譯器來執行 echo hello world ec...

Linux下Shell指令碼

shell 指令碼 shell script 是一種為 shell 編寫的指令碼程式。從業界所說的 shell 通常都是指 shell 指令碼,但讀者朋友要知道,shell 和 shell script 是兩個不同的概念。由於習慣的原因,簡潔起見,本文出現的 shell程式設計 都是指 shell ...