特殊:Badtitle/NS100:Beginners/BashScripting/zh
文章出处: |
{{#if: | {{{2}}} | http://wiki.ubuntu.org.cn/UbuntuHelp:Beginners/BashScripting }} |
点击翻译: |
English {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/af | • {{#if: Beginners/BashScripting|Afrikaans| [[::Beginners/BashScripting/zh/af|Afrikaans]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/ar | • {{#if: Beginners/BashScripting|العربية| [[::Beginners/BashScripting/zh/ar|العربية]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/az | • {{#if: Beginners/BashScripting|azərbaycanca| [[::Beginners/BashScripting/zh/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/bcc | • {{#if: Beginners/BashScripting|جهلسری بلوچی| [[::Beginners/BashScripting/zh/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/bg | • {{#if: Beginners/BashScripting|български| [[::Beginners/BashScripting/zh/bg|български]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/br | • {{#if: Beginners/BashScripting|brezhoneg| [[::Beginners/BashScripting/zh/br|brezhoneg]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/ca | • {{#if: Beginners/BashScripting|català| [[::Beginners/BashScripting/zh/ca|català]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/cs | • {{#if: Beginners/BashScripting|čeština| [[::Beginners/BashScripting/zh/cs|čeština]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/de | • {{#if: Beginners/BashScripting|Deutsch| [[::Beginners/BashScripting/zh/de|Deutsch]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/el | • {{#if: Beginners/BashScripting|Ελληνικά| [[::Beginners/BashScripting/zh/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/es | • {{#if: Beginners/BashScripting|español| [[::Beginners/BashScripting/zh/es|español]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/fa | • {{#if: Beginners/BashScripting|فارسی| [[::Beginners/BashScripting/zh/fa|فارسی]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/fi | • {{#if: Beginners/BashScripting|suomi| [[::Beginners/BashScripting/zh/fi|suomi]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/fr | • {{#if: Beginners/BashScripting|français| [[::Beginners/BashScripting/zh/fr|français]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/gu | • {{#if: Beginners/BashScripting|ગુજરાતી| [[::Beginners/BashScripting/zh/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/he | • {{#if: Beginners/BashScripting|עברית| [[::Beginners/BashScripting/zh/he|עברית]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/hu | • {{#if: Beginners/BashScripting|magyar| [[::Beginners/BashScripting/zh/hu|magyar]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/id | • {{#if: Beginners/BashScripting|Bahasa Indonesia| [[::Beginners/BashScripting/zh/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/it | • {{#if: Beginners/BashScripting|italiano| [[::Beginners/BashScripting/zh/it|italiano]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/ja | • {{#if: Beginners/BashScripting|日本語| [[::Beginners/BashScripting/zh/ja|日本語]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/ko | • {{#if: Beginners/BashScripting|한국어| [[::Beginners/BashScripting/zh/ko|한국어]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/ksh | • {{#if: Beginners/BashScripting|Ripoarisch| [[::Beginners/BashScripting/zh/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/mr | • {{#if: Beginners/BashScripting|मराठी| [[::Beginners/BashScripting/zh/mr|मराठी]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/ms | • {{#if: Beginners/BashScripting|Bahasa Melayu| [[::Beginners/BashScripting/zh/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/nl | • {{#if: Beginners/BashScripting|Nederlands| [[::Beginners/BashScripting/zh/nl|Nederlands]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/no | • {{#if: Beginners/BashScripting|norsk| [[::Beginners/BashScripting/zh/no|norsk]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/oc | • {{#if: Beginners/BashScripting|occitan| [[::Beginners/BashScripting/zh/oc|occitan]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/pl | • {{#if: Beginners/BashScripting|polski| [[::Beginners/BashScripting/zh/pl|polski]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/pt | • {{#if: Beginners/BashScripting|português| [[::Beginners/BashScripting/zh/pt|português]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/ro | • {{#if: Beginners/BashScripting|română| [[::Beginners/BashScripting/zh/ro|română]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/ru | • {{#if: Beginners/BashScripting|русский| [[::Beginners/BashScripting/zh/ru|русский]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/si | • {{#if: Beginners/BashScripting|සිංහල| [[::Beginners/BashScripting/zh/si|සිංහල]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/sq | • {{#if: Beginners/BashScripting|shqip| [[::Beginners/BashScripting/zh/sq|shqip]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/sr | • {{#if: Beginners/BashScripting|српски / srpski| [[::Beginners/BashScripting/zh/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/sv | • {{#if: Beginners/BashScripting|svenska| [[::Beginners/BashScripting/zh/sv|svenska]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/th | • {{#if: Beginners/BashScripting|ไทย| [[::Beginners/BashScripting/zh/th|ไทย]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/tr | • {{#if: Beginners/BashScripting|Türkçe| [[::Beginners/BashScripting/zh/tr|Türkçe]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/vi | • {{#if: Beginners/BashScripting|Tiếng Việt| [[::Beginners/BashScripting/zh/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/yue | • {{#if: Beginners/BashScripting|粵語| [[::Beginners/BashScripting/zh/yue|粵語]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/zh | • {{#if: Beginners/BashScripting|中文| [[::Beginners/BashScripting/zh/zh|中文]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/zh-hans | • {{#if: Beginners/BashScripting|中文(简体)| [[::Beginners/BashScripting/zh/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: Beginners/BashScripting | Beginners/BashScripting | {{#if: | :}}Beginners/BashScripting/zh}}/zh-hant | • {{#if: Beginners/BashScripting|中文(繁體)| [[::Beginners/BashScripting/zh/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:Beginners/BashScripting|:Beginners/BashScripting/zh|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :Beginners/BashScripting/zh/zh | | {{#ifexist: Beginners/BashScripting/zh/zh | | {{#ifeq: {{#titleparts:Beginners/BashScripting/zh|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:Beginners/BashScripting/zh|1|-1|}} | zh | | }}
Bash 脚本是最容易学习的脚本类型之一,它十分类似于Windows下的批处理脚本. Bash非常的灵活, 而且它有很多你在批处理中无法看到的高级特性. 但如果你对电脑一无所知,那么它对你来说毫无用处. 一般来说,你会喜欢学习Bash,因为大多数在Ubuntu下的日常工作,都可以用终端来解决. 你也会渐渐明白大部分事情都可以通过用户图形界面和命令行来完成, 然而有些事情选择其中一种方法要更容易一些. 例如,使用命令行来更改一个文件夹及其子文件夹中的文件的权限要比使用用户图形界面容易的多.
Intro
In this document we will discuss useful everyday commands, as well as going a bit more in depth into scripting and semi-advanced features of Bash. Bash is not only used to run programs and applications, but it can also be used to write programs or scripts.
Bash -- Everyday Ubuntu life
During your time as an Ubuntu'er you will use the terminal to perform tasks such as
- Creating folders
- Deleting files
- Deleting folders and their sub-folders
- Opening applications as root
- Backing up your files
- Backing up your folders
- Checking system performance
- Check Devices
- Checking wireless connection
And many other things, the list above will be the commands we will discuss.
Commands
Creating folders
Creating folders can be done simply in the file manager nautilus by right clicking and selecting 'Create Folder', but if you want to do this from a cli environment you would type the following in the terminal:
sudo mkdir /home/joe/Desktop/new_folder
the mkdir (make directory) command creates the folder then the file path tells it where to create the folder. (I use sudo because if you try to create a folder anywhere other then /home or one of its sub-directories you will need root/superuser privileges. This goes for all commands in this section.)
Deleting files
Deleting files can be done like this
sudo rm /home/joe/file_to_be_deleted
the rm (remove) command is used to remove anything through a cli environment.
Deleting folders and their sub-folders
The command you are about to read can potentially (if used incorrectly) destroy your system!
sudo rm -rf /home/joe/useless_Parent_folder
This command is slightly different to the one before, it uses two options '-r' which means recursive (will delete the folder and all sub-folders) and '-f' means force (will not ask for your permission). This command is perfectly fine for deleting a dir and all its sub-dirs. The next commands should !!**!!NEVER!!**!! be run. Unless you want to say goodbye to your system.
sudo rm -rf /* sudo rm -rf /
This will delete everything from your root folder downwards, which if you did a standard install would be everything.
Opening applications as root
Sometimes you will want to edit a config file in your root folder, in order to save changes to this file you need root privileges so we need to open our text editor as root. (assuming your text editor is gedit)
gksudo gedit /path/to/conf_file.txt
gksudo is the same as sudo only it brings a box up to ask for your password, gksudo should always be used to open graphical applications as root. Serious damage can be done to your system by editing these files. It is advised to create a backup of any file you edit.
Backing up your files
To create a backup of a file, we're going to use the cp (copy) command. The basic syntax for cp is as follows:
cp source_file dest_file
This will copy the 'source_file' to the 'dest_file'. Now, using the previous example, we want to backup '/path/to/conf_file.txt'. To accomplish this, we type the following:
sudo cp /path/to/conf_file.txt /path/to/conf_file.txt.old
That's fine and dandy, but what if I want to copy my file to another directory? Well that's just as easy. Let's say instead of copying /path/to/conf_file.txt to the /path/to/ directory, you want to copy it to a directory where you store all of your backup files, say /my/backup/folder/. In order to accomplish this you would type:
sudo cp /path/to/conf_file.txt /my/backup/folder/ #saves conf_file.txt to /my/backup/folder/ #OR sudo cp /path/to/conf_file.txt /my/backup/folder/conf_file_new_name.txt
- This is a typical safety measure that has saved many users in the past from a complete disaster.***
Okay, so we know how to copy a file a) to a different filename and b) to a different folder. But how do we copy entire directories?
Backing up your directories
To backup one directory to another, we introduce cps '-r' (recursive) option. The basic syntax is as follow:
cp -r /directory/to/be/copied/ /where/to/copy/to/
So if we wanted to copy all of the contents of our /path/to/ folder to our /my/backup/folder, we would type the following:
sudo cp -r /path/to/ /my/backup/folder/foldername #foldername can be whatever you want the foldername to be
Checking system performance
If your computer starts to lag, you can see which applications are using the most CPU power with this command:
top
This is generally the same information given as the GUI application 'System Monitor'.
Check Devices
USB Devices If a USB device stops working, you may want to see if it is still connected/detected. To check if a device is connected/detected, type the following:
lsusb
PCI Devices PCI devices are checked with:
lspci
Checking wireless connection
To check the status on your wireless connection, use:
iwconfig
This also shows packets sent/received.