Linux Shell Script Study

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.txt
PROMPT>echo this is a sample text>&4
PROMPT>cat output.txt 
this is a sample text
PROMPT>

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.sh
echo -n Count:
tput sc

count=0;
while true;
do
    if [ $count -lt 40 ];
    then
        let count++;
        sleep 1;
        tput rc
        tput ed
        echo -n $count;
    else exit 0;
    fi
done

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值