- 用 sed 替换字符串中指定字符,例如替换 / 为 -
%>echo abc/123 | sed 's/\//-/'
abc-123
- 将 echo 的输出结果赋值给一个变量 new
%>set new=`echo abc/123 | sed 's/\//-/'`
%>echo $new
abc-123
- #!/bin/bash下,判断多个文件是否同时存在
if [ -f $bom_list ] && [ -f $exclu_list ]; then
#!/bin/csh下,
if ( -r $dir/$file) then
若文件名包含空格,需要用引号,多个文件是否存在,或的关系如下:
if ( -f "$filename1" || -f "$filename2") then
- #!/bin/bash下,判断路径是否存在
if [ ! -d $build_dir ]; then
echo "Error: Directory Not found ! - "$build_dir
exit
fi
#!/bin/csh下,判断路径是否存在
if ( -d $build_dir ) then
echo "Error: Directory Not found ! - "$build_dir
exit
endif
- 判断字符串是否为空
if ( ! -z "$source_headers" ) then
- 判断文件大小不为零
if ( -s $dir/$file) then
- 循环读取文件中的每一行,csh:
foreach line ( "`cat $file`" )
set filename = ( $line )
echo $filename
end
bash:
while read line; do
echo $line
done < $file
- 循环读取文件中的每一行并计数,每次加1,bash:
i=0
while read line; do
echo $line
((i++))
done < $file
echo "Total number of lines: $i"
- shell 求和,bash:
num1=90
num2=10
let num=num1+num2
echo $num
-
关系运算符
原博地址 https://www.runoob.com/linux/linux-shell-basic-operators.html
-
布尔运算符
-
逻辑运算符
-
字符串运算符
-
文件测试运算符
-
#! /bin/csh -f 循环读取文件夹名称
#! /bin/csh -f
foreach foldername ( "`ls /home/project/`" )
echo $foldername
end
- #! /bin/csh 下定义一个list并循环读取
#!/bin/bash
projectList=(p1 p2 p3)
echo "projectList = "${projectList[*]} //显示list全部内容
for i in ${projectList[*]}; do
echo $i
done