Linux Internship Report - Experiment 2 Shell and Shell Programming

Internship purpose

  1. Learn shell functions and related configuration files, and Bash common commands
  2. Learn how to write shell scripts
  3. Learn to write simple Bash scripts
  • Internship Requirements
  1. Learn about Bash related configuration files: /etc/profile, $HOME/.bash_profile, $HOME/.bashrc, $HOME/.bash_logout
  2. Master Bash common commands: history, alias, input and output redirection
  3. Understand the format of shell scripts
  4. Master the functions of common metacharacters in Bash
  5. Master the concept of variables in Bash: the difference between user variables and environment variables, copying and referencing of variables, and commonly used built-in variables and environment variables in Bash
  6. Master Bash built-in commands: eval, echo, exec
  7. Master the arithmetic operation let, conditional test test and [ ] and various tests and logical operations in Bash
  8. Master Bash's control structures: if, for, case
  9. Understand Bash's functions, point commands
  • Internship Content
  1. Exercise the command about the shell in the textbook
  2. Modify Bash's configuration file to let all users log in to display all the content in the user's home directory first, and give a welcome speech.

 Vim .bash_profile starts the vi editor and type after it;

Ls -a displays all files in the user's home directory;

Echo “welcome to come in!” Displays the welcome word.

3. Save the last 20 command lines used by a user to a file, edit them with vi, and delete the serial number before the command line.

history 20 >>one.txt saves the last 20 command lines used by a user to one.txt;

vim one.txt starts the editor and deletes the serial number;

cat one.txt to view file information.

4. Add 5 command aliases for a user (specific commands are customized), and make them available directly after each login.

alias a='ls -a'

alias l='ls -l'

alias mk=mkdir

alias e=echo

alias w=wc

5. Analyze the following script functions (comment each statement and describe the script function):

username=$1//Assign the first positional parameter to the variable username.

[ -z $username ]&&read -p “Please Input Username:” username

//If the username value is empty, assign the username again through the keyboard, and prompt the message "Please Input Username:".

w -h|grep -q “^$username” || { echo $username do not login;exit 1;}

//Check whether the user with the username value is logged in, if not, output "username do not login;exit 1;".

echo -n Input Message to Send:// displays "Input Message to Send:" on the screen without newlines.

read msg//Read the keyboard input and save it to the msg variable.

if echo $msg|write $username 2>/dev/null //Send the content in the msg variable to the user specified by the username value, and store the error message in /dev/null, which is not displayed on the screen.

       then echo Send Message successfully!!//If the sending is successful, the message will be displayed on the screen.

       else echo Send Message fail!!//If the sending fails, this message will be displayed.

fi//Script function, send the specified information to the specified user, if the user is not logged in, it will display that the user is not logged in, otherwise, the specified information will be sent to the user, and whether it is successful or not will be displayed.

6. Write a script to count the number of common files, directories, and linked files in the specified directory.

#!/bin/bash
[ $# -eq 0]&&{
echo 指定待统计目录的列表
exit 1;
}
fa=`ls -l"$@"|grep "^-"|wc -l`
fb=`ls -l "$@"|grep "^d"|wc -l`
fc=`ls -l "$@"|grep "^l"|wc -l`
echo "普通文件:$fa 目录文件: $fb 链接文件:$fc"
  1. Write a script named backup.sh, copy the files ending with .c and .sh in the directory specified by the second positional parameter and subsequent parameters, and the file size is not 0, to the first positional parameter. in the specified directory. (Note: It is required to check the validity of the positional parameters)

8. Write the script mytar.sh to archive and compress all the files (directories) specified by the location parameter. The file name format is: year-month-day (xxxx-xx-xx).tar.gz. The number of position parameters required to be detected, if it is 0, an error message is given.

vim mytar.sh starts the vi editor and names the script mytar.sh;

Enter the command code in the vi editor;

Enter chmod a+x mytar.sh on the command line;

./mytar.sh feb.c archive and compress the file.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324356808&siteId=291194637