linux学习记录(一)

1.打印字符串
abc@ubuntu:~$ echo "welcome to bash"
welcome to bash
abc@ubuntu:~$ echo 'text in quotes'
text in quotes
(在ubuntu下转义符失效)
abc@ubuntu:~$ echo "Hello World \!"
Hello World \!

2.编写简单脚本测试打印语句 
abc@ubuntu:~$ cd ~/Downloads
abc@ubuntu:~/Downloads$ mkdir study
abc@ubuntu:~/Downloads$ cd study
abc@ubuntu:~/Downloads/study$ vi printf.sh
(输入以下内容)
#!/bin/bash

#FileName: printf.sh


printf "%-5s %-10s %-4s\n" No Name Mark
printf "%-5s %10s %-4.2s\n" 1 Sarath 80.329586
printf "%-5s %-10s %-4.2f\n" 2 James 90.32849231
printf "%-5s %-10s %-4.2f\n" 3 Jeff 3.8219325
abc@ubuntu:~/Downloads/study$ chmod a+x ./printf.sh
abc@ubuntu:~/Downloads/study$ ./printf.sh
No    Name       Mark
1         Sarath 80  
2     James      90.33
3     Jeff       3.82

3.使用制表符输出
abc@ubuntu:~/Downloads/study$ echo -e "1\t2\t3"
1 2 3

3.控制文本显示不同的颜色(以下三行在ubuntu下可显示不同颜色)
abc@ubuntu:~/Downloads/study$ echo -e "\e[1;31m This is red text \e[0m"
 This is red text 
abc@ubuntu:~/Downloads/study$ echo -e "\e[1;32m This is red text \e[0m"
 This is red text 
abc@ubuntu:~/Downloads/study$ echo -e "\e[1;33m This is red text \e[0m"
 This is red text 

4.查看进程pid以及查看与其相关的环境变量
abc@ubuntu:~/Downloads/study$ pgrep ssh
13147
13150
13213
abc@ubuntu:~/Downloads/study$ sudo cat /proc/13147/environ
[sudo] password for abc: 
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/binTERM=linuxSSH_SIGSTOP=1UPSTART_JOB=sshUPSTART_INSTANCE=abc@ubuntu:~/Downloads/s'udy$ sudo cat /proc/13147/environ | tr '\0' '\n 
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
TERM=linux
SSH_SIGSTOP=1
UPSTART_JOB=ssh
UPSTART_INSTANCE=


5.使用变量
1)
#!/bin/bash
#FileName:variables.sh
fruit=apple
count=5
echo "We have $count $(fruit}(s)"

2)
abc@ubuntu:~/Downloads/study$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games":/usr/local/jdk1.7.0_79/bin
export PATH="$PATH:/home/user/bin"
abc@ubuntu:~/Downloads/study$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games":/usr/local/jdk1.7.0_79/bin:/home/user/bin

6.查看当前使用的shell
abc@ubuntu:~/Downloads/study$ echo $SHELL
/bin/bash
abc@ubuntu:~/Downloads/study$ echo $0
-bash

7.查看当前的UID
abc@ubuntu:~/Downloads/study$ echo $UID

1000





9.修改命令提示符
abc@ubuntu:~/Downloads/study$ PS1="PROMPT>"


10.以命令修改系统变量
PROMPT>sudo mkdir -p /opt/myapp/bin
PROMPT>export PATH=/opt/myapp/bin:$PATH
PROMPT>echo $PATH
/opt/myapp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games":/usr/local/jdk1.7.0_79/bin:/home/user/bin
PROMPT>echo $LD_LIBRARY_PATH


11.使用函数
PROMPT>prepend(){ [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; } 
PROMPT>sudo mkdir -p /opt/myapp/bin_test
PROMPT>prepend PATH /opt/myapp/bin_test
PROMPT>echo $PATH
/opt/myapp/bin_test:/opt/myapp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games":/usr/local/jdk1.7.0_79/bin:/home/user/bin


12.加法示例
PROMPT>cd ~/Downloads
PROMPT>cd study
PROMPT>vi app.sh
#!/bin/bash
no1=38;
no2=99;
let result=no1+no2;
echo $result
PROMPT>./add.sh
137

你可能感兴趣的:(linux,ubuntu)