---
layout: post
title: IC设计中Linux shell的选择和使用
description: IC设计中Linux shell的选择和使用
keywords: bash、tcsh、zsh
author: admin
date: 2020-08-22 15:49
category: 网络技术
tags: shell
---


你很可能正在使用bash,也可能是csh。你可能听说过sh、tcsh。如果你安装过EDA,就知道某EDA要正常工作必须要装ksh。另外你可能还听某大神安利过zsh……

## 一图看懂各种shell的联系与区别

那这么多shell有什么联系和区别,我们先来看看它们的历史和关系。


## ICer该如何选择shell

那么这么多shell,我们ICer该如何选择呢?看以下几条信息:
- 在现代化的操作系统中,比如Solaris11、RHEL7、Mac OS X默认都是bash。
- IC企业里用tcsh的居多,因为大家原先都在Solaris里干活,老版本的Solaris默认是csh。
- 在RHEL6、7里,sh软链接指向bash,csh软链接指向tcsh。
- zsh很流行,颜色很好看,自动化功能很强大,特别是Oh My Zsh的扩展使用zsh成为了终极shell。可惜的是,很多芯片设计公司服务器没有安装,周围也没有同事用。
- EDA安装目录里有ksh脚本,说明有一些IC企业在用ksh,但国内用的人极少。

基于以上的分析,对于普通ICer,我们的结论是:
- 如果你是普通搬砖的ICer,有啥用啥,周围同事用啥你也用啥。所以bash和tcsh都得学。
- 如果你是公司的领导(Leader、经理、中层管理等),你让手下用啥,手下就用啥。需要考虑生产力,传统,学习培训成本。
- 在家,你爱用啥用啥,看着舒服,用着顺手就行。

另外,由于Tcl、Perl、Python等脚本语言的强大,我们只需要了解和掌握shell的常见、简单用法即可。把复杂的编程交给Tcl/Perl/Python去做吧。

## shell简易教程

注释

```bash
# this is comment
```

用户配置文件

```bash
#bash
~/.bashrc

#tcsh
~/.tcshrc 或 ~/.cshrc

#zsh
~/.zshrc
```

脚本第一行指定解释器

```bash
#!/bin/bash
#!/bin/tcsh
#!/bin/zsh
```

变量定义,赋值,引用

```bash
#bash
CompanyInfo="AI Chips, Co. LTD"
EDA_HOME=/tools/eda
echo $EDA_HOME
my_array=(1 2 3 4)
echo ${my_array[0]} #output is 1
my_hash['first']=1
my_hash[second]=2
echo ${my_hash[first]}

#tcsh
CompanyInfo = "AI Chips, Co. LTD"
set EDA_HOME = /tools/eda
echo $EDA_HOME
set my_array = (1 2 3 4)
echo $my_array[1] #output is 1

#zsh
CompanyInfo="AI Chips, Co. LTD"
EDA_HOME=/tools/eda
echo $EDA_HOME
my_array=(1 2 3 4)
echo $my_array[1] #output is 1
decare -A my_hash
my_hash['first']=1
my_hash[second]=2
echo $my_hash[first]
```

Tips:
1. bash数组下标从0开始,tcsh和zsh从1开始。
2. bash引用关联数组用${my_hash[first]},tcsh和zsh不需要{},直接$my_hash[first]。

环境变量

```bash
#bash, zsh
export NOVAS_HOME=/tools/eda/synopsys/novas
或者
NOVAS_HOME=/tools/eda/synopsys/novas
export NOVAS_HOME

#tcsh
setenv NOVAS_HOME /tools/eda/synopsys/novas
```

Tips:
1. tcsh的path数组只对当前shell有效。但如果写在用户配置文件~/.tcshrc里,path也是对所有shell有效的。

条件

```bash
#bash, zsh
foo="a"
if [ $foo = "a" ]; then
echo "equal"
fi

#csh
set foo = "a"
if($foo == "a")then
echo "equal"
endif
```

Tips:
1. 考虑兼容性,bash、zsh中的字符串比较用=,而tcsh里用==
2. bash、zsh的[ ]内部必须有空格

循环

```bash
#bash
arr=("a" "b" "c")
for((i=0; i<${#arr[*]}; i++))
do
echo ${arr[$i]}
done

#tcsh
set arr = (a b c)
foreach i ($arr)
echo $i
end

#zsh
arr=("a" "b" "c")
echo $arr
for ((i=1; i <= $#arr; i++)) {
echo $arr[$i]
}
for i ($arr) {
echo $i
}
```

Tips:
1. 数组长度的表示方式不同,bash里用${#arr[*]}或者${#arr[@]},tcsh和zsh用$#arr
2. bash用for var in {0..9},tcsh用foreach var ($array),zsh用for var ($array)

函数

```bash
#bash, zsh
function sum(){
return $(($1+$2))
}
sum 1 2
echo "sum = $?"
```

Tips:
1. 函数必须先定义后使用
2. 函数传入参数时,在函数内部用$1, $2, $3等取得
3. return的返回值保存在内置变量$?里
4. 函数调用时,参数直接写在函数名后面,与linux命令的格式相同
5. tcsh不支持函数的语法,可以用alias实现类似函数的功能

下面是tcsh里用alias实现类似求和函数sum的例子。alias只是起到简化代码的作用,相当于宏定义。

```bash
#tcsh
alias sum '@ sum = $a + $b'; echo $sum
set a = 1
set b = 2
sum #output is 3
set b = 10
sum #output is 11
```

## 更多资料推荐

bash
菜鸟教程
[http://www.runoob.com/linux/linux-shell.html](http://www.runoob.com/linux/linux-shell.html)
IBM Bash实例教程
[https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-1/index.html](https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-1/index.html)
[https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-2/index.html](https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-2/index.html)
[https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-3/index.html](https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-3/index.html)
GNU Bash参考手册
[http://www.gnu.org/software/bash/manual/bash.html](http://www.gnu.org/software/bash/manual/bash.html)

tcsh
IBM Tcsh shell变量
[https://www.ibm.com/developerworks/cn/aix/library/au-tcsh/index.html](https://www.ibm.com/developerworks/cn/aix/library/au-tcsh/index.html)
tcsh - unix, linux command
[http://www.tutorialspoint.com/unix_commands/tcsh.htm](http://www.tutorialspoint.com/unix_commands/tcsh.htm)
杜克大学的一篇c shell教程
[https://www2.cs.duke.edu/csl/docs/csh.html](https://www2.cs.duke.edu/csl/docs/csh.html)

zsh
陌辞寒的zsh开发指南
[https://github.com/goreliu/zshguide](https://github.com/goreliu/zshguide)
池建强的zsh的介绍、安装和配置
[http://macshuo.com/?p=676](http://macshuo.com/?p=676)