Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Shell programming entry     - Use py2exe to generate exe files Python script (Programming)

- Build Golang development environment configuration on Ubuntu 14.04 (Linux)

- Build Python3.4 + PyQt5.5.1 + Eric6.1.1 development platform under Mac OS X 10.11.1 (Server)

- Android custom controls create the simplest skid menu in the history (Programming)

- Prevent security threats caused Rootkit (Linux)

- Java garbage collection and heap memory layout (Programming)

- GAMIT10.5 under CentOS installation (Linux)

- Golang use Oracle database on Ubuntu 14.04 (Linux)

- Oracle table space rename and delete table space (Database)

- ASM Disk Space Check (Database)

- Linux kernel update error, update-initramfs: failed Solution (Linux)

- IOS distributed management using GitHub project development (Linux)

- For the FreeBSD install Adobe Flash plug-in (Linux)

- Linux server alarms using Java (Server)

- Zookeeper Rights Management and Quota Management (Server)

- Linux process group, session daemon (Linux)

- Some safety precautions of Linux servers (Linux)

- Linux Network Programming - raw socket programming (Programming)

- Modify MySQL character encoding under Linux (Database)

- Android Fragment really fully resolve (Programming)

 
         
  Shell programming entry
     
  Add Date : 2017-08-31      
         
         
         
  Background knowledge

Shell is a user interface to interact with the kernel, and is one of the most important software Linux. The most popular of Shell called bash Shell, bash Shell script programming with its simple, efficient and known over the years become a Linux programmers and system administrators to solve practical problems of the weapon. Shell is the outermost layer of the operating system. Shell combined programming language to control processes and files, as well as start and control other programs. Shell by prompting you to enter, to interpret the input to the operating system, and then deal with any resulting output from the operating system to manage the interaction between you and the operating system. Shell provides a way to communicate with the operating system. This communication interactively (input from the keyboard immediately operation) or as a Shell script execution. Shell and Shell scripts are a sequence of operating system commands, it is stored in a file. When you log into the system, the system locates the name of Shell to be executed. After its execution, Shell displays a command prompt. Ordinary users this prompt is usually a $ (dollar sign). When you enter the command prompt and press Enter, Shell commands to be evaluated, and try to execute it. Depending on the command instructions, Shell command output is written to the screen or to redirect output. It then returns to the command prompt and waits for you to enter another command. The command line is where the input line, which includes Shell prompt. The basic format of each line is as follows:

$ Command parameters (one or more)

The first word (up to the first space) Shell, as the command line as a command, all subsequent words as arguments.

1: Determine whether the file is a block device or character device files, if you copy it to / dev directory

#! / Bin / bash

read -p "please input a filename:" filename
if test -b "$ filename" -o -c "$ filename"; then
    echo "$ filename is a device file" && cp $ filename / dev
else
    echo "$ filename is not a device file"
fi

2: Analog Linux Login Shell

#! / Bin / bash

x = wj # This is the user name
y = 1111 # This is the password
read -p "please input yourname:" yourname
read -p "please input yourkey:" yourkey
if test "$ x" = "$ yourname" -a "$ y" = "$ yourkey"; then bash # attention test later compare the size of the space should not be spared
else
echo "please try again"
fi

3: Read the two numbers from the keyboard, and compare the size of two numbers, and print the results

#! / Bin / bash

read -p "please input num1:" num1
read -p "please input num2:" num2
if test $ num1 = $ num2; then
echo "num1 = num2"
elif test $ num1 -gt $ num2; then
echo "num1> num2"
else test $ num1 -lt $ num2
echo "num1 fi


 

4: Find / the existence of the specified file under root / directory

#! / Bin / bash

cd / root
read -p "please input filename:" filename
if test -e $ filename; then
    echo "$ filename is exist"
else
    echo "$ filename is not exist"
fi

5: Delete the current directory under the file size 0

# / Bin / bash

for filename in `ls`
do
  if test -d $ filename; then
b = 1
else
a = $ (ls -l $ filename | awk '{print $ 5}')
  if test $ a -eq 0; then
rm $ filename
  fi
  fi
done

6: Find the maximum file

#! / Bin / bash

a = 0
for name in *
do
  if test -d $ name; then
c = 1
  else
b = $ (ls -l $ name | awk '{print $ 5}')
if test $ b -ge $ a; then
a = $ b
          namemax = $ name
      fi
    fi
done
echo "the max file is $ namemax"

7: Print the current user (not whoami)

#! / Bin / bash


echo "Current User is:" echo $ (who | sed -n '1p' | awk '{print $ 1}')

8: Finding the current directory of all the files larger than 50k, these file names written to a text file, and count the number thereof

#! / Bin / bash

find ./ -size + 50k> toingji.txt
cat tongji.txt | wc -l

9: a function using the calculated shift parameter product of all, assuming that the parameters are integers

#! / Bin / bash

result = 1
# $ # Represents the number of parameters
while test $ # -gt 0
do
    result = `expr $ 1 \ * $ result`
    shift
done
echo "the result is $ result"

10: Testing whether a file has been sorted

#! / Bin / bash

read -p "please input your filename:" file
sort -C $ file
# $? Represents the execution of a command return value (display the exit status of last command .0 no error, any other value indicates an error)
a = $?
if test $ a -eq 0; then
echo "$ file is youxu"
else
echo "$ file is wuxu"
fi

11: to determine whether the user is running

#! / Bin / bash

a = $ (ps | grep "$$" | awk '{print $ 2}')
b = $ (who | grep "$ a" | awk '{print $ 1}')
read -p "please input a username:" username
if [ "$ b" = "$ username"]; then
    echo "the ueser is running"
else
    echo "the ueser is not running"
fi

12: In the current directory interactively create a folder (the folder already exists, the print prompt for a new name)

#! / Bin / bash

read -p "please input dirname:" file
if test -d $ file; then
    echo dir is exist please try again
else
    read -p "create dir now? [y / n]" sel
    if test "$ sel" = "y"; then
        mkdir $ file
    elif test "$ sel" = "n"; then
        bash
    else
        echo "Your input is wrong"
    fi
fi

13: Get the local IP address

#! / Bin / bash

a = $ (ifconfig | grep "inet address:" | awk '{print $ 2}' | sed 's / Address: // g' | sed -n '1p')
echo "ip is $ a"

14: Print user without password

#! / Bin / bash

echo "no passwd user are:"
a = $ (sudo cat / etc / shadow | grep "*" | awk 'BEGIN {FS = ":"} {print $ 1}')
# Before processing the input file, field separator (FS) is set to a colon
echo $ a

15: Write a script using the iterative calculation n factorial

#! / Bin / bash

read -p "please input n:" n
sum = 1
for a in `seq 1 $ n`
do
      sum = `expr $ sum \ * $ a`
done
echo "n! = $ sum"

16: Using case statement tests whether the input characters: lowercase / uppercase / Digital / Other

#! / Bin / bash

read -p "please input something:" Key
case $ Key in
        [[: Lower:]]) echo "It is lowercase" ;;
        [[: Upper:]]) echo "It is uppercase" ;;
        [0-9]) echo "It is number" ;;
esac

17: Test whether the file contains a particular text

#! / Bin / bash

read -p "enter a string:" string
read -p "enter a filename:" filename
grep -q "$ string" $ filename # Find the file in str (not found in output), the successful return 0
if test $ -eq 0;? then
    echo "The text exists in the file"
else
    echo "Text does not exist in the file"
fi

18: detecting whether a given word dictionary words

#! / Bin / bash

read -p "enter a string:" string
# / Usr / share / dict / american-english dictionary files Ubuntu system commonly used in British English word list
grep -q "$ string" / usr / share / dict / american-english
if test $ -eq 0;? then
    echo "match directionary"
else
    echo "not match directionary"
fi
     
         
         
         
  More:      
 
- Based on Google Cloud Storage Mirroring open Maven Central Repository (Server)
- Gitblit adopted Ticket collaborative development model (Linux)
- Compiling source code Nginx module installation subs_filter (Server)
- Oracle SQL statement to retrieve data paging table (Database)
- Configuring xdebug debugging environment in Ubuntu 14.04 under the PhpStorm (Linux)
- MySQL multi-master multi-cluster deployment uses alive Galera (Database)
- Linux (Debian) install software, missing dynamic link libraries .so (Linux)
- php How to prevent being injected (Linux)
- How to Check notebook CPU temperature in Ubuntu (Linux)
- Linux System Getting Started Learning: Linux common log file (Linux)
- How to determine whether the Linux server was hacked (Linux)
- Linux Getting Started Tutorial: Ubuntu laptop screen brightness adjustment (Linux)
- How to configure a development environment elegant Lua (Linux)
- Oracle 10046 Event (Database)
- Install multiple Linux distributions and Fedora 21 first experience on the same hard disk (Linux)
- ARM assembler instruction debugging method (Programming)
- grep, egrep and regular expressions (Linux)
- Restore Oracle Database Cold backup and database reconstruction emca (Database)
- Java Generics (Programming)
- KVM virtualization nested configuration (Server)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.