linux下使用alias和PS1命令

一、实验环境:


安装了Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统。有另外一个无特权用户student,密码student 的账户存在。


二、实验要求:


1、使用alias 创建别名

2、使用PS1 修改bash 的提示符

3、编写简单的shell 小程序


三、实验步骤:


创建别名c:

[student@localhost~]$ alias c='clear'
[student@localhost~]$ alias
    alias l.='ls -d .*--color=auto'
    alias ll='ls -l--color=auto'
    alias ls='ls--color=auto'
    alias vi='vim'

运行别名c:

[student@localhost~]$ c

此时的别名没有保存,注销后就别名就会丢失。要使这个别名在用户student 每次登录都能使用,需要保存。


修改.bashrc文件,保存创建的别名:

[student@localhost~]$ vim .bashrc

原文件显示如下:

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
         . /etc/bashrc
fi
# User specific  aliases and functions

在文件最后添加一行:

alias c='clear'

保存退出后,注销重新登录,别名c仍然可以使用。

 

使用PS1命令改变bash提示


[student@localhost~]$ PS1='Red Hat-> '

Red Hat-> ls

修改bash恢复为主机名和传统美元符号

Red Hat->  PS1='\h $'


修改成功:

localhost $


恢复历史提示:

localhost $ PS1='[\u@\h\w(\!)]$'


修改成功:

[student@localhost~(16)]$


编写一个shell脚本,当你输入yes时返回no


#!/bin/bash
#if you enter yes it will echo no
#if you enter no it will echo yes
 
echo "you want YES or NO:"
read ANSWER
 
$vi test_script
if["$ANSWER"="YES"]||["$ANSWER"="yes"];then
       echo"Your idea is No";
elif["$ANSWER"="NO"]||["$ANSWER"="no"];then
       echo"Your idea is YES";
else
       echo"you are wrong";
fi


使用./运行脚本test_script,但是权限不足

$./test_script 
-bash:./test_script: Permission denied

ll查看该文件没有执行权限

[student@localhost~(20)]$ll test_script 
-rw-rw-r-- 1student student 304 Aug 26 05:02 test_script

为这个文件增加执行权限

[student@localhost~(21)]$chmod u+x test_script

[student@localhost~(22)]$ ll test_script 
-rwxrw-r-- 1student student 304 Aug 26 05:02 test_script


再次运行

[student@localhost~(23)]$./test_script
 
You want YES or NO:
YES
Your idea is NO



你可能感兴趣的:(linux,alias创建别名,PS1修改提示符)