c shell 学习记录(csh)
基础总结
1)定义变量
- 1、通过set来定义局部变量x,通过 x 或 者 x或者 x或者{x}来使用, %x 表示变量的值的长度, ?x来判断变量x是否设置,如果设置则为1,否则为0
set x = 5
echo $x
echo ${x}kg
echo $%x
全局变量的定义setenv v value 该变量将被此shell派生的所有子shell继承。
$$表示当前进程的PID, $status 或 $? 表示退出状态。
2)定义数组
- 定义数组myarr, 通过 m y a r r [ i n d e x ] 来 访 问 数 组 中 的 值 , 注 意 i n d e x 是 从 1 开 始 的 。 通 过 myarr[index]来访问数组中的值,注意index是从1开始的。通过 myarr[index]来访问数组中的值,注意index是从1开始的。通过myarr或 m y a r r [ ∗ ] 来 访 问 数 组 所 有 的 元 素 。 通 过 myarr[*]来访问数组所有的元素。通过 myarr[∗]来访问数组所有的元素。通过#myarr来查看元素的个数。
- ()中加空格,索引myarr[2]的时候就是一个,不然就是两个
set myarr = (str1, str2,str3)
echo $myarr[2]
echo $myarr
echo $myarr[*]
3)命令替换
通过set x = cmd
来执行命令,且结果赋值给变量。
set d = `date`
echo $d
echo $d[6]-$d[2]-$d[3]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hfAIjfHk-1657424824490)(E:\picgo\imga_copy\image-20220709162849023.png)]
4)命令行参数
通过 a r g v [ 1 ] , argv[1], argv[1],argv[2]或$1, 2 来 访 问 命 令 行 参 数 。 命 令 行 参 数 的 个 数 为 2来访问命令行参数。命令行参数的个数为 2来访问命令行参数。命令行参数的个数为#argv。
5)文件名扩展的元字符
只能使用?,*,[abc],[a-c]。
6)IO重定向和管道
将命令的输出重定向到文件为>。
将命令的输出重定向并追加到文件为>>。
将命令的输入重定向到文件为<。
将命令的报错信息重定向到一个文件(cmd>/dev/tty)>&errors。
将命令的输出和错误输出分别重定向(cmd > goodstuff) >& badstuff。
将命令的输出和报错信息重定向到一个文件cmd>&file。
将命令的输出经管道发往另一个命令cmd|cmd.
将命令的输出和报错信息经管道发往另一个命令cmd|&cmd。
条件语句为 cmd && cmd 或 cmd || cmd。
command<<WORD 表示将command的输入重定向为从第一个WORD处开始,到下一个WORD处之间的内容(即here文档)。
7)从键盘读取并保存到变量中
set var = $<
8)算术
@ var = 5 + 5
echo $var
@ v2 = $var + 3
echo $v2
9) label 和 goto
csh中没有函数的概念,使用类似windows批处理中的label和goto。
goto label
......
label:
....
10) if/else/switch/case
if(expression)thencommands
endifif {(command)} thencommands
endifif(expression) thencommands
else if(expression) thencommands
elsecommands
endifswitch("$value")
case pattern1:commandsbreaksw
case pattern2:commandsbreaksw
default:commandsbreaksw
endsw
11)while/foreach
while(expression)commandscontinuebreak
endforeach var (wordlist)commands
end
12)repeat
repeat表示重复执行后面的命令。
repeat 3 echo "helloworld"
13)csh中设置环境变量PATH的方法
csh中使用path代替PATH,设置类似于数组的使用。
set path = ($path /home)
echo $path
echo $PATH
14)用`可以将一条命令包裹起来
set ret=`pwd` echo ${ret}
条件语句
cshell 命令语法
1.if / then / else
if (expr) then commands else if (expr2) then commands else commands endifExample:#!/bin/cshif ($#argv == 0) then echo "No number to classify" else if ($#argv > 0) then set number = $argv[1] if ($number < 0) then @ class = 0 else if (0 <= $number && $number < 100) then @ class = 1 else if (100 <= $number && $number < 200) then @ class = 2 else @ class = 3 endifecho The number $number is in class $classendif
2.foreach / end
Syntax:
foreach name (wordlist)commandsend
Example:
#!/bin/cshforeach color (red orange yellow green blue)echo $colorend
3.while / end
Syntax:
while (expression)commandsend
Example:
#!/bin/cshset word = "anything"while ($word != "")echo -n "Enter a word to check (Return to exit): "set word = $<if ($word != "") grep $word /usr/share/dict/wordsend
4.break
Syntax:
break
Example:
#!/bin/cshforeach number (one two three exit four)if ($number == exit) thenecho reached an exitbreakendifecho $numberend
Syntax:
continue
Example:
#!/bin/cshforeach number (one two three exit four)if ($number == exit) thenecho reached an exitcontinueendifecho $numberend
5.goto
Goto 语句将控制权转移到以 label 开头的语句:
Syntax:
goto label
Example:
#!/bin/cshif ($#argv != 1) goto error1if ($argv[1] < 6) goto error2goto OKerror1:echo "Invalid - wrong number or no arguments"echo "Quitting"exit 1error2:echo "Invalid argument - must be greater than 5"echo "Quitting"exit 1OK:echo "Argument = $argv[1]"exit 1
6.单引号与双引号表示不同的作用
单引号:
允许包含空格,
防止变量替换,
允许生成文件名
双引号:
允许包含空格
允许变量替换
允许生成文件名
#!/bin/cshset opt=-lset x1='ls $opt'echo $x1set x2="ls $opt"echo $x2Will produce the output:ls $optls -l
7.存储命令的输出
通过反引号(ESC下面那个)进行特殊命令的存储,并且通过索引进行调用
Syntax:
set variable = `command`
Example:
#!/bin/cshset date_fields=`date`echo $date_fieldsecho $date_fields[1]echo $date_fields[2]foreach field(`date`)echo $fieldendSample output:Thu Mar 9 22:25:45 HST 1995ThuMarThuMar922:25:45HST1995
8.读取用户输入
set variable = $<- or -set variable = `head -1`
然后就需要用户自己在命令行输入对应的数组
#!/bin/cshecho -n Input your value: set input = $<echo You entered: $input- or -#!/bin/cshecho -n Input your value: set input = `head -1`echo You entered: $input
简单个例
设置一个数组名为ages,赋予变量(0 0 0 0 0)
$ set ages = (0 0 0 0 0)
将ages中的第二个数值定义为15
$ @ ages[2] = 15
将ages中的第三个数值定义为第二个数值加上4
$ @ ages[3] = ( $ages[2] + 4 )
输出这个数组第三个数值
$ echo $ages[3]
输出数组的所有结果
$ echo $ages
c shell 循环语句:If语句
If Example:
#!/bin/csh -f
# Set class depending on argument value
set number = $argv[1]
if ($number < 0) then @ class = 0 else
if ($number >= 0 && $number < 100) then @ class = 1
else if ($number >= 100 && $number < 200) then @ class = 2
else @ class = 3
endif
echo The number $number is in class $class
c shell 循环语句:switch语句
#!/bin/csh -f
switch (string variable) case pattern: commandsbreakswcase pattern: commandsbreaksw:default:commandsbreaksw
endsw
c shell 循环语句:while语句
@ limit = $argv[1] @ index = 1 @ sum = 0 while ($index <= $limit) @ sum += $index @ index++ end echo The sum is $sum
定义一个变量,使用命令:
set var1=a3 #sets var1's value to a3.
set var2=(a b c) # sets the array variable var2 to a b, and c.
通过在变量名前面键入美元符号($)来使用变量。如果变量是一个数组,则可以使用括号[]指定下标,并且可以使用 $# var2格式获取元素的数量
要分配计算值,使用@命令如下:
@var = $a + $x * $z
对比bash&cshell
Bourne shell
#!/bin/sh
i=2
j=1
while [ $j -le 10 ]
doecho '2 **' $j = $ii=`expr $i '*' 2`j=`expr $j + 1`
done
#!/bin/sh
if [ $days -gt 365 ]
thenecho This is over a year.
fi
#!/bin/sh
for i in d*
docase $i ind?) echo $i is short ;;*) echo $i is long ;;esac
done
C shell
#!/bin/csh
set i = 2
set j = 1
while ( $j <= 10 )echo '2 **' $j = $i@ i *= 2@ j++
end
#!/bin/csh
if ( $days > 365 ) thenecho This is over a year.
endif
#!/bin/csh
foreach i ( d* )switch ( $i )case d?:echo $i is shortbreakswdefault:echo $i is longendsw
end
c shell 学习记录(csh)相关推荐
- Linux+shell学习记录和思维导图
由于shell和Linux学习分不开,所以干脆一起结合起来学习,顺便用思维导图工具做一个记录. 学习的关键在于对着教程敲代码. 学习工具 思维导图工具Xmind:以前一直用百度脑图做一些简单的记录,但 ...
- bash shell 学习记录
定义变量(中间没有空格) a=8 使用变量 echo $a 计算加法(注意空格) a=8 b=2 c=`expr $a + $b` echo $c 如果是乘法,需要将 * 转义 c=`expr $a ...
- shell编程学习记录
shell学习记录(持续更新中--) 创建shell脚本文件 与用户交互-echo 输入输出重定向 输出重定向 输入重定向 内联输入重定向 shell中变量的定义和使用 管道符 | 脚本中的数学运算 ...
- linux个人学习记录
linux学习记录 资料: Linux 黑马程序员_bilibili AcWing Linux基础课 可能是东半球最全面易懂的 Tmux 使用教程! Shell 教程 | 菜鸟教程 (runoob.c ...
- (一)Git学习记录(不断更新)
作为程序员如果你还不知道 Git 和 GitHub,说不过去吧,赶紧来学习一波. 一.认识GitHub Git 是个版本控制系统,说明白点就是进行代码的各种管理,比如你写错代码进行回滚啊.追寻 Bug ...
- MongoDB学习记录:入门(一)——五叶草
预热看我之前的文章Node学习记录: mongodb 这个系列旨在系统的学习Mongodb 部分图片来自慕课网mongodb入门截图 学习目标 MongoDB官网:https://www.mongod ...
- M001: MongoDB Basics Chapter 3: Deeper Dive on the MongoDB Query Language学习记录
M001: MongoDB Basics Chapter 3: Deeper Dive on the MongoDB Query Language学习记录 运行环境 操作系统:windows 10 家 ...
- Markdown个人学习记录
Markdown个人学习记录 参考书籍:<了不起的Markdown>-毕小朋-电子工业出版社 第一章 Markdown概述 Markdown作为现在最流行的轻量级"写作语言&qu ...
- Linux shell:sh csh tcsh ash bash dash zsh
Shell :贝壳,寓意类似内核的壳,Shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务.Shell 是一个用 C 语言编写的程序,它是用户使用 Linux ...
最新文章
- 推荐100份:高并发高可用和中台一网打尽
- 如何优雅地展示机器学习项目!
- 让你明白response.sendRedirect()与request.getRequestDispatcher().forward()区别
- WIN32下(非MFC)自编TRACE
- 自定义函数_python3基础07函数(自定义)
- java读取yaml配置文件,snakeyaml读取yaml配置文件
- DataSet与DataReader的区别
- synchronized原理_浅谈synchronized的实现原理
- excel文件损坏修复绝招_修复数据工具大盘点,让你快速掌握电脑数据恢复的秘密武器...
- From Intrusion Detection to Attacker Attribution: A Comprehensive Survey of Unsupervised Methods翻译
- 【建模算法】Python调用Gurobi求解TSP问题
- 两个椭圆的公切线求法(Matlab)
- html5捕鱼达人源码分享!!!
- SAP中状态参数文件最高状态和最低状态的理解
- 白话解读“中台”技术
- UCenter 1.6 数据字典
- termios的例子
- iOS 图片转base64编码
- 使用无人机进行视频直播
- 如何在本机上拥有虚拟机软件(Linux系统)(如何在本机上安装虚拟镜像)