C advanced day3

 

1. Write a script named myfirstshell.sh, which includes the following content.

1. Include a note listing your name, the name of the script, and the purpose of writing this script

2. Say "hello username" to the current user

3. Display your machine name hostname

4. Display a list of all files in the upper directory

5. Display the values ​​​​of the variables PATH and HOME

6. Display disk usage

7. Use the id command to print out your group ID id -g

8. Say “Good bye” to users

#!/bin/bash 
#echo ubuntu myfirstshell Job
#Say hello to the user name
echo "hello `whoami` "
#Display the machine name hostname
echo `hostname`
#Display the list of all files in the upper directory 
echo `ls ..`
#Display the values ​​of variables PATH and HOME
echo "$PATH $HOME"
#Display disk usage
echo `df -h`
#Use ID command to print group id
var5=`id -g`
echo $var5
#Tell users GOODBYE
var6= `whoami`
echo "GOODBYE $var6"

2. Write a script ~/sumfile.sh to count how many first-level subdirectories and files there are in /etc, /var, and /usr directories

# Count the number of all first-level subdirectories and files in /etc, /var, /usr directories

etc_count=$(find /etc/* -maxdepth 0 | wc -l)

var_count=$(find /var/* -maxdepth 0 | wc -l)

usr_count=$(find /usr/* -maxdepth 0 | wc -l)

echo "Number of first-level subdirectories and files in the /etc directory: $etc_count"

echo "Number of first-level subdirectories and files under the /var directory: $var_count"

echo "Number of first-level subdirectories and files under the /usr directory: $usr_countl"

3. Write a shell script to get the current user name, user id and working path

#!/bin/bash 
var=`whoami`
var1=`grep "ubuntu" /etc/passwd | cut -d ":" -f "3"`
echo "username:$var userid:$var1 working path: pwd "
 

4. Count the number of files starting with P or p in the /etc directory - c counts the line number

find /etc -maxdepth 1 -type f | grep -ic "^/etc/p"

5. Enter a file name to determine whether the file is an ordinary file

#!/bin/bash

read -p "Please enter the file name: " filename

# Determine whether the input file is an ordinary file

if [ -f "$filename" ]

 then

echo "$filename is a normal file"

else echo "$filename is not a regular file"

fi

Guess you like

Origin blog.csdn.net/m0_53451387/article/details/130631701