Linux Shell Script Study

来源:互联网 发布:php访问mysql数据库 编辑:程序博客网 时间:2024/06/05 18:24

1. Linux Shell 光标移动操作

Alt+b--->将光标向后移动一个单词

Alt+d--->删除光标前面的一个单词

alt+Backspace--->删除光标前面的一个单词

如果要一次连续删除几个单词,可以设置操作的参数(argu),例如要删除3个单词,则操作如下:

a.先设置操作的参数:alt+3:

brandon@ubuntu:~$ PS1="PROMPT>"PROMPT>PROMPT>echo "this is a sample text." > temp.txt(arg: 3) echo "this is a sample test." > temp.txt
然后执行要调用此命令参数的命令:alt+Backspace
结果:PROMPT>echo "this is a sample 
这个方法可以应用到别的操作中
</pre><pre name="code" class="cpp">ctrl+d--->删除光标后面的一个字符

ctrl+a--->光标移动到命令行的行首

ctrl+t--->交换光标前的两个字母(对容易打错英文单词的人非常有用)


2. Linux Shell 重定向

即让命令行又让命令在终端显示时,使用 tee 命令, tee -a 命令,可以防止overwrite所写的文件

输出重定向(redirection),操作符“>”和“>>”:

操作符“>”    :首先清空文件,然后写入。

操作符“>>”  :将输出添加到已经存在的文件的后面,不擦除原有文件内容。

3.alias, exec命令

alias命令可以帮助我们重命名一些命令,alias 命令的有效性是暂时的,如果关掉当前终端就会失效。如果想永久有效,可以添加到~/.bashrc 文件中

例如:

alias install='sudo apt-get install'

echo 'alias install="sudo apt-get install" ‘ >> ~/.bashrc

取消别名的时候可以调用unalias命令。或者直接从.bashrc文件中删除。

alias rm $@=


exec命令可以让我们自定义文件描述符,默认是0,1,2分别代表 stdin, stdout, stderr,

例如:

PROMPT>exec 4>output.txtPROMPT>echo this is a sample text>&4PROMPT>cat output.txt this is a sample textPROMPT>

3. shell 中常用到的运算符:

Other important operators are as follows:

  • -gt: Greater than
  • -lt: Less than
  • -ge: Greater than or equal to
  • -le: Less than or equal to

4. 一个小的延时程序:

#!/bin/bash#Filename: sleep.shecho -n Count:tput sccount=0;while true;do    if [ $count -lt 40 ];    then        let count++;        sleep 1;        tput rc        tput ed        echo -n $count;    else exit 0;    fidone

tput sc 命令用来保存当前光标的位置(save cursor position), let count++,即使 count的值在loop中+1. sleep 1; 即 延迟1s. tput rc, 将光标回复到原来的位置(restore cursor position), tput ed 命令清除从当前光标位置到设备末尾的数据。





0 0