【shell】C shell

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lbt_dvshare/article/details/94170199

csh -vx  used to display the script as it is replaced and the input variables used to help debugging.

variable

  • By set to define local variables x, the value used by the variable $ x x (or $ {x});
  • $% x   length of the value of the variable ;
  • ? $ x    to determine the variable x is set, such as setting was 1, and 0 otherwise;
set x = 5
 
echo ${x}   # ​echo ${x}, echo $%x 

 Global variables (also known as system variables ) is defined setenv all sub-shell v value of the variable will be derived in this shell of inheritance.


unsetenv  MINUS_SRC_USER  
setenv    MINUS_SRC_USER    dv01055

 Variable modifiers

: r root return variable.
: e return extended variable.
: h return header variables.
: t tail return variable.
: gr returns all roots.
: ge Return all extensions.
: gh Return all of the head.
: gt return all the tail.
: q will cause the variable to a list of words, to which the parts are separated. When the variable contains the file name of the meta-characters should not be extended, the more useful options.
: x to cause a pattern, and extend it into a list of words.

set aa=(/progs/num.c /book/chap.ps)

变量部分     格式                 输出结果
正常变量     echo $aa              /progs/num.c /book/chap.ps
第二个根     echo $aa[2]:r         /book/chap
第二个头     echo $aa[2]:h         /book
第二个尾     echo $aa[2]:t         chap.ps
第二个扩展   echo $aa[2]:e         ps
根           echo  $aa:r          /progs/num /book/chap.ps
全部根       echo  $aa:gr          /progs/num /book/chap
头           echo   $aa:h          /progs /book/chap.ps
全部头       echo   $aa:gh         /progs /book
尾           echo  $aa:t           num.c /book/chap.ps
全部尾       echo   $aa:gt         num.c chap.ps
扩展         echo   $aa:e          c  /book/chap.ps
全部扩展     echo $aa:ge           c  ps

Array

  •  Define arrays myarr, to access the values in the array by $ myarr [index], note 1 starting from the index .
  • By $ myarr or $ myarr [*] to access an array of all elements.
  •  To view the number of elements by $ # myarr.
set myarr = (str1, str2, str3)
 
​echo $myarr[2]    # echo $myarr  echo $myarr[*]

Command substitution

     By set x = `cmd` to execute the command, and the result is assigned to the variable.

set d = `date`
 
​echo $d   # echo $d[6]-$d[2]-$d[3]

Command line parameters

  • [1], $ argv [2] or $ 1, $ 2, $ argv access command line arguments.
  • The number of command line parameters is $ # argv.

Command mode

   cmd1  &&   cmd2       

  •  Logic and relations, the implementation of cmd1 (if cmd1 executed successfully) and then perform cmd2.
  • This is a short-circuit operation, if there is no successful implementation cmd1, cmd2 will never be executed.   

    cmd1  ||  cmd2

  • Or logical relationship or perform cmd1 (if execution fails cmd1) CMD2;
  • This is a short-circuit operation, if cmd1 executed successfully, cmd2 will never be executed. 

Control Flow

    (1)  Label sum goto

      

goto label
 
  ​......
 
​label:
 
  ​....

    (2)if/else/switch/case

 if(xxx) then
 
   commands
 
 endif

  if(xxx) then
 
    commands
 
  else if(xxx) then
 
    commands
 
  else
 
    commands
 
  endi
switch("$value")
 
  case pattern1: commands1  breaksw
 
  case pattern2: commands2  breaksw
 
  default: commands  breaksw
 
endsw

while(xxx)
 
  commands
 
  continue
 
  break
 
end

 


foreach var (wordlist)
 
  commands
 
end
repeat 3 "echo helloworld"

break:   Continue distance while or foreach recently, the command after the end of the command.

breaksw : termination of a switch statement, and then continue to execute the command after endsw.


eval args

Generally, for the eval shell scripts, and args is a line code comprising a shell variables. eval first mandatory variable expansion, then run the command generates.

When the shell comprises a variable input / output redirection symbols, aliases, or other shell variable, which is useful in two searches. (For example, under normal circumstances redirection occurs before variable expansion, so a variable containing redirection symbols must first eval extension, otherwise it will not explain the redirection symbol.

set b=,$a,
set a=hello
echo $b 读命令行一次
$a

eval echo $b 读命令行两次
hello

 


File Operations

   - Operators filename

      -e file exists returns 1 (.true.), otherwise it returns 0 (.false.).

      -r readable file returns 1, otherwise it returns 0.

      -w .... write ................

      -x .... executable ...............

      -o file belonging to the user himself returns 1, otherwise it returns 0.

      -z file length of 0 returns 1, otherwise it returns 0.

      1 -f file returns to normal (plain) file, 0 otherwise.

      -d file is a directory file returns 1, otherwise 0
 

Guess you like

Origin blog.csdn.net/lbt_dvshare/article/details/94170199