C-shell excellent program

Source: Internet
Author: User
C-shell excellent program-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. switch usage. Note that each case must end with breaksw
Otherwise, the next case command will be executed.
(1) In addition, $ <means to obtain the user's stand input
(2) If the-n option is added to echo, the cursor will stay at the end of the row.

Echo-n "Input one color :"
Set STOPLIGHT =$ <
Switch ($ STOPLIGHT)
Case red:
Echo "red"
Breaksw
Case orange:
Echo "orange"
Breaksw
Case green:
Echo "green"
Breaksw
Default:
Echo "you input $ STOPLIGHT"
Endsw

--------------------------------------------------------------------
2. Use set to obtain the variable, set ABC = "I am ABC"
You can also use 'command' to obtain the command.
Besides, case can also be replaced by a ten-character *.

Set VER = 'uname-R'
Switch ($ VER)
Case 5.5:
Echo "run the setup of $ VER"
Breaksw
Case 5.3:
Echo "run the setup of $ VER"
Breaksw
Case 5 .*:
Echo "like 5.x"
Breaksw
Case 4 .*:
Echo "like 4.x"
Breaksw
Default:
Echo "no idea"
Endsw

--------------------------------------------------------------------
3. if syntax, compare numbers

Set n1 = 1
Set n2 = 2
If ($ n1 = $ n2) then
Echo "$ n1 Equal $ n2"
Else
Echo "$ n1 Not Equal $ n2"
Endif


--------------------------------------------------------------------
4. if syntax, comparison string

Set n1 = abcdef
Set n2 = abcde
If ($ n1 = $ n2) then
Echo "$ n1 Equal $ n2"
Else
Echo "$ n1 Not Equal $ n2"
Endif


--------------------------------------------------------------------
5. if syntax, similar strings

Set n1 = abcdef
Set n2 = abcde
If ($ n1 = ~ $ N2) then
Echo "$ n1 Like $ n2"
Else
Echo "$ n1 Not Like $ n2"
Endif


--------------------------------------------------------------------
6. if syntax, compare the number size

Set n1 = 1
Set n2 = 2
If ($ n1> $ n2) then
Echo "$ n1> $ n2"
Else
Echo "$ n1 <$ n2"
Endif


--------------------------------------------------------------------
7. programs executed once per minute

# Mm is equal to the number of [minutes] of the current day
Set mm = 'date | cut-d'-f4 | cut-d:-f2'

If (-r $ 0.out) then
Rm $ 0.out
Touch $ 0.out
Else
Touch $ 0.out
Endif

While ($ mm <= 16)
Set mm = 'date | cut-d'-f4 | cut-d:-f2'
Echo "$ mm now is 'date '"
Sleep 60
# E cho "$ mm now is 'date'" >>$ 0.out
End
Echo "Over" >>> 0.out


--------------------------------------------------------------------
8. An example of loop and the use of expr for adding
The loop syntax is as follows:
Foreach number (1 2 3)
Echo $ number
End

Set counter = 0
While ($ counter <= 10)
Echo "sleeping for 5 seconds"
Sleep 5
Counter = 'expr $ counter + 1'
End


--------------------------------------------------------------------
9. Set a program that uses the month and date of the current day as the file name
For example, if today is 10/02, $ prefix will be equal to the program + 1002
Date. csh1002

Set prefix = 'basename $ 0' 'date' + % m % d''
Echo $0
Echo $ prefix


--------------------------------------------------------------------
10. Remove the font string from the specified file in the foreach Loop


Foreach file ([B, e, g, h, s] *. html)
Echo-n "Processing $ file, remove the line number 'grep-n font $ file '"

# $ Log indicates that this $ file contains several font strings
Set log = 'grep-c font $ file'
If ($ log = '0') then
Echo ", pass $ file"
Else
# First find the number of rows that appear font for the first time of the file. If 3, $ cmd = 3d
Set cmd = 'grep-n font $ file | cut-d:-f1 | head-1 'd
# Use sed to execute the delete action and output the result to $ {file} 1
Sed $ cmd $ file >$ {file} 1
# If $ {file} 1 has no information, passing
If (-z $ {file} 1) then
Echo ", $ {file} 1 is zero"
Else
Cp $ {file} 1 $ file
Rm {$ file} 1
Echo ", $ file remove OK"
Endif
Endif
End

# Later I saw the further usage of sed and found that the previous writing was too stupid. Try this
# Sed/font/d $ file >$ {file} 1
# Once OK, I am really stupid

--------------------------------------------------------------------
11. function: add Xxxx

Foreach file (sky *. html)
Set filetitle = ftitle
# The main part is the sed part s/^ * //, indicating to delete the blank space before the first character of the row
Echo" 'Grep back $ file | head-1 | sed-e's/^ *//''"> $ Ftitle

# Insert the line just now.
Head-1 $ file >$ {file} head
Sed 1d $ file >$ {file} 1
Cat $ ftitle >>> {file} head
Cat $ {file} 1 >$ {file} head
Cp $ {file} head $ file
Rm $ {file} 1
Rm $ ftitle
Rm $ {file} head

Echo "$ file OK"
End

--------------------------------------------------------------------
12. A program that actually creates an ftp server
It contains many applications and is of reference value.
(Unfinished)

Set path = (/usr/bin/usr/sbin)
#
Set true = 'grep-c ftp/etc/passwd'
If ($ true = 0) then
Echo "no ftp user in your system"
Echo-n "do you want to create the ftp user? "
Set answer =$ <
If ($ answer = 'y' | $ answer = 'y') then
Set maxid = 'sort/etc/passwd | tail-1 | cut-d:-f3'
Echo $ maxid
Set newid = 'expr $ maxid + 1'
Echo $ newid
Echo "/usr/sbin/useradd-d/home1/ftp-u $ newid-s/etc/false ftp"
Endif
Else
Echo "Good. Your system already has the ftp user ."
Set ftphome = 'grep ftp:/etc/passwd | cut-d:-f6'
Echo $ ftphome
Endif

If (-z $ ftphome) then
Echo "ftphome must be non-null"
Exit 2
Endif

If ($ ftphome = "/usr" | $ ftphome = "/") then
Echo "ftphome can't be/or/usr"
Exit 2
Endif

# Create the ftp home directory
If (! -D $ ftphome) then
Echo "mkdir $ ftphome"
Endif

Echo "Setting up the ftphome for SunOS 'uname-R '"

If (! -D $ ftphome) then
Echo "mkdir-p $ ftphome/usr/bin"
Endif

Cp/bin/ls $ ftphome/usr/bin

Chmod 111 $ ftphome/usr/bin/ls
Chown root $ ftphome/usr/bin
Chmod 555 $ ftphome/usr/bin

If (-r $ ftphome/bin) then
Mv-f $ ftphome/bin $ ftphome/Obin
Endif
Ln-s usr/bin $ ftphome


--------------------------------------------------------------------
13. Obtain the UID of the user

If ($ # argv = 0) then
Echo "$0 usage: $1 username"
Exit 2
Endif

Set uid = 'grep $1/etc/passwd | cut-d:-f3'
Echo $ uid


--------------------------------------------------------------------
14. Replace html in the specified file with htm

Foreach file (*. html)
Echo "Processing $ file ..."
Sed s/html/htm/$ file >$ {file} 1
Cp $ {file} 1 $ file
Rm $ {file} 1
End


--------------------------------------------------------------------
15. A simple example. Just look at it.

#! /Bin/csh-f
Echo .................
Echo WELCOME to \ * tape copy \*
Echo .................
Echo Enter your name:
# $ <Can read from stand input
Set name = $ <
Echo ""
Echo Hi $ name \!
Set D = 'date'
Echo Today \'s date is $ D [1] $ D [2] $ D [3]
If ($ D [1] = Mon) then
Echo -------------------------------------------------------------
Echo Today is $ D [1] day $ name, it \'s time to copy your directorys \!
Echo -------------------------------------------------------------
Else
Echo -------------------------------------------------------------
Echo Today is $ D [1] day $ name, no tape copies today \!
Echo -------------------------------------------------------------
Endif


--------------------------------------------------------------------
16. A finger Program

Set FINGER = "/usr/ucb/finger"

If (-x $ FINGER) then
If ($ # argv = 0) then
Cat <TAG
---------------------------------
Hahahah ....
---------------------------------
TAG
Else
$ FINGER "$ *"
Endif

Else
Echo "Cannot find finger on this system ."
Endif


--------------------------------------------------------------------
17. Methods for obtaining Variables

Set W = 'Who-R'
Echo $ W [9]



--------------------------------------------------------------------
18. Change the file name and Set *. html --> *. htm

# Rename *. html to *. htm
Echo-n "This will change *. html to *. htm. Can I continue? (Y/n ):"
Set input =$ <
If ($ input! = "Y" & $ input! = "Y") then
Echo "OK. Quit ..."
Exit 2
Endif

Foreach file (*. html)
Echo "Processing $ file to 'basename $ file .html'.htm"
Mv $ file 'basename $ file .html'.htm
End

--------------------------------------------------------------------
19. Change the file name and Set *. htm --> *. html

Echo-n "This will change *. htm to *. html. Can I continue? (Y/n ):"
Set input =$ <
If ($ input! = "Y" & $ input! = "Y") then
Echo "OK. Quit ..."
Exit 2
Endif

# Rename *. htm to *. html
Foreach file (*. htm)
Echo "Processing $ file to 'basename $ file .htm'.html"
Mv $ file 'basename $ file .htm'.html
End

--------------------------------------------------------------------
20. Change the archive name in upper case to lower case.
Tr string1 string2 will set the string of standard input,
All corresponding string1 are replaced by string2

Foreach file (*)
Mv $ file 'echo $ file | tr' [A-Z] ''[a-z]''
End

--------------------------------------------------------------------
21. Change the lower-case file name to the upper-case file name.

Foreach file (*)
Mv $ file 'echo $ file | tr' [a-z] ''[A-Z]''
End
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.