个人工具

“UbuntuHelp:Beginners/BashScripting/zh”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第5行: 第5行:
  
 
Bash 脚本是最容易学习的脚本类型之一,它十分类似于Windows下的批处理脚本. Bash非常的灵活, 而且它有很多你在批处理中无法看到的高级特性. 但如果你对电脑一无所知,那么它对你来说毫无用处. 一般来说,你会喜欢学习Bash,因为大多数在Ubuntu下的日常工作,都可以用终端来解决. 你也会渐渐明白大部分事情都可以通过用户图形界面和命令行来完成, 然而有些事情选择其中一种方法要更容易一些. 例如,使用命令行来更改一个文件夹及其子文件夹中的文件的权限要比使用用户图形界面容易的多.
 
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:
 +
<pre><nowiki>
 +
sudo mkdir /home/joe/Desktop/new_folder
 +
</nowiki></pre>
 +
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
 +
<pre><nowiki>
 +
sudo rm /home/joe/file_to_be_deleted
 +
</nowiki></pre>
 +
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!
 +
<pre><nowiki>
 +
sudo rm -rf /home/joe/useless_Parent_folder
 +
</nowiki></pre>
 +
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.
 +
<pre><nowiki>
 +
sudo rm -rf /*
 +
sudo rm -rf /
 +
</nowiki></pre>
 +
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)
 +
<pre><nowiki>
 +
gksudo gedit /path/to/conf_file.txt
 +
</nowiki></pre>
 +
''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:
 +
<pre><nowiki>
 +
cp source_file dest_file
 +
</nowiki></pre>
 +
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:
 +
<pre><nowiki>
 +
sudo cp /path/to/conf_file.txt /path/to/conf_file.txt.old
 +
</nowiki></pre>
 +
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:
 +
<pre><nowiki>
 +
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
 +
</nowiki></pre>
 +
***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 ''cp''s '-r' (recursive) option.  The basic syntax is as follow:
 +
<pre><nowiki>
 +
cp -r /directory/to/be/copied/ /where/to/copy/to/
 +
</nowiki></pre>
 +
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:
 +
<pre><nowiki>
 +
sudo cp -r /path/to/ /my/backup/folder/foldername    #foldername can be whatever you want the foldername to be
 +
</nowiki></pre>
 +
=== Checking system performance ===
 +
If your computer starts to lag, you can see which applications are using the most CPU power with this command:
 +
<pre><nowiki>
 +
top
 +
</nowiki></pre>
 +
This is generally the same information given as the GUI application 'System Monitor'.
 +
=== Check Devices ===
 +
<u>USB Devices</u>
 +
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:
 +
<pre><nowiki>
 +
lsusb
 +
</nowiki></pre>
 +
<u>PCI Devices</u>
 +
PCI devices are checked with:
 +
<pre><nowiki>
 +
lspci
 +
</nowiki></pre>
 +
=== Checking wireless connection ===
 +
To check the status on your wireless connection, use:
 +
<pre><nowiki>
 +
iwconfig
 +
</nowiki></pre>
 +
This also shows packets sent/received.

2009年1月27日 (二) 23:08的版本


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.