Example of Shell programming under Linux

Source: Internet
Author: User

1. Determine if a file is a block or character device file, and if it is copied to the/dev directory


Read-p "Input a file:" filename

If [-B $filename-O-C $filename]
Then
CP $filename/dev/

Fi


2. Write a script to perform a simple subtraction operation requiring prompt input variable


#!/bin/bash
read-p "input a number:" Num1
Read-p "input another Number: "Num2
Let" num3=num1-num2 "
Echo $num 3



3. Change the file name extension in a directory to bat, and then compress and pack the files to a directory in time.


#!/bin/bash
for file with $ (LS $)
do
New_file=${file%.*}.bat
mv./$1/$file./$1/$new _file
tmp=$ (date +%y)
tar cvf./$tmp. Tar./$1
Done

4. Determine if a number is a number. Prints the number of finishes between 1-1000. The number is twice times the number of the sum equal to itself. (6,28,496)

#!/bin/bash
Sub ()
{
I=1;
sum=0;
While [$i-le $num]
does
let "m=num%i"
If [$m-eq 0]
and
Let "sum=sum+i"
Fi
Let "i=i+1"
Don E
Let ' a=2*num '
if [$a-eq $sum]
then
Echo $num
Fi
}
num=1
While [$num-le +]
do
sub< Br>let "num = num+1"
Done


5. Find a file with a specified string under a folder

#!/bin/bash
For file in $ (LS $)
Do
bname=$ (grep-l $ $2/$file)
BaseName $bname
Done


6. Add a new group for Class1, and then add 30 users belonging to this group, with the user name in the form stdxx, where xx is from 01 to 30.

#!/bin/bash
Groupadd Class1
For i in {9901..9930}
Do
xx=$ (echo $i | sed ' s/99//');
useradd-g Class1 Std$xx-p ""
Done


7. Design a shell program, in the/userdata directory to establish 50 directories, that is, USER1~USER50, and set the permissions of each directory, its \ The other user's permissions are: Read, the file owner's permissions are: Read, write, execute, the permissions of the group of the file owner is: Read, execute.

#!/bin/bash
For ((i=1;i<=50;i++))
Do
Mkdir-p/usrdata/user$i
Cd/usrdata
chmod 754 user$i
Done


8. Write a script that calculates the factorial of 10 using a loop
#!/bin/sh
Factorial=1
For a in ' SEQ 1 10 '
Do
factorial= ' expr $factorial \* $a '
Done
echo "10! = $factorial "

9. Delete a file of size 0 under the current directory
#/bin/bash
for filename in ' ls '
Do
If test-d $filename
Then b=0
Else
a=$ (ls-l $filename | awk ' {print $} ')
if test $a-eq 0
Then RM $filename
Fi
Fi
Done



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.