Shell Scripting Exercises

Source: Internet
Author: User
Tags parent directory disk usage

The new handwriting of the shell, write bad, please forgive us.

Hope to make some great gods and peers. qq:86416192 welcome everyone to add QQ.

1, write a script, show how many users, and display the ID of each user.

#!/bin/bash


File= "/etc/passwd"

lines= ' Wc-l $file | Cut-d ""-f1 "

For I in ' seq 1 $LINES ';

Do

Userid= ' head-$I $file | Tail-1 |cut-d:-f3 '

Username= ' head-$I $file | Tail-1 |cut-d:-f1 '

echo "Hello $username, your UID is $userid"

Done

echo "There are $LINES users"


Test results

[Email protected] scripts]# sh test.sh

Hello Root,your UID is 0

Hello Bin,your UID is 1

Hello Daemon,your UID is 2

Hello Adm,your UID is 3

Hello Lp,your UID is 4

Hello Sync,your UID is 5

Hello Shutdown,your UID is 6

Hello Halt,your UID is 7

Hello Mail,your UID is 8

Hello Operator,your UID is 11

Hello Games,your UID is 12

Hello Ftp,your UID is 14

Hello Nobody,your UID is 99

Hello Avahi-autoipd,your UID is 170

Hello Systemd-bus-proxy,your UID is 999

Hello Systemd-network,your UID is 998

Hello Dbus,your UID is 81

Hello Polkitd,your UID is 997

Hello Abrt,your UID is 173

Hello Tss,your UID is 59

Hello Postfix,your UID is 89

Hello Sshd,your UID is 74

Hello Rsync,your UID is 1000

Hello Chrony,your UID is 996

There are users




2, in the root directory has four files M1.txt,m2.txt,m3.txt,m4.txt, with Shell programming, to achieve the automatic creation of M1,M2,M3,M4 four directories, and will M1.txt,m2.txt,m3.txt, M4.txt four files are copied to each corresponding directory.

#!/bin/bash

CD/

Touch M1.txt m2.txt m3.txt m4.txt

I=1

While [$I-le 4]

Do

mkdir m$i

CP M$i.txt M$i

i=$ ((i+1))

Done




3. Write a script called myfirstshell.sh, which includes the following content.

A) contains a comment that lists your name, the name of the script, and the purpose of writing the script.

b) Greeting the user.

c) Displays the date and time.

d) display the calendar for this month.

E) Displays the name of your machine.

f) Displays the name and version of the current operating system.

g) Displays a list of all files in the parent directory.

h) Shows all processes that root is running.

i) Displays the value of the variable term, path, and home.

j) Display of disk usage.

k) Print out your group ID with the ID command.

m) say "good bye" with the user



#!/bin/bash

echo "Li Songyang writing"

User= ' WhoAmI '

Case $user in

Root

echo "Hello root";;

Teacher

echo "Hello teacher";;

*)

echo "Hello $user, welcome"

Esac echo "Date and Time: ' Day '"

echo "This month's calendar: ' Cal '"

echo "Machine Name of the machine: ' Uname-n '"

echo "Current name and version of this operating system: ' Uname-s;uname-r '"

echo "List of all files in the parent directory: ' ls '. /`"

echo "Root is running all processes: ' Ps-u root '"

echo "Variable term value: $TERM"

echo "Variable PATH value: $PATH"

echo "Variable HOME value: $HOME"

echo "Disk usage: ' DF '"

echo "Print out your group ID with the ID command: ' id-g '"

echo "Good bye!"




4, design a shell program, in the/userdata directory to establish 50 directories, that is, USER1~USER50, and set the permissions for each directory rwxr-xr-


#!/bin/bash


if [-d/userdata]

Then

Cd/userdata

Else

Mkdir-p/userdata && Cd/userdata

Fi


I=1


While [$I-le 50]

Do

Mkdir-p user$i

chmod 754 user$i

i=$ ((i+1))

Done


5, write Shell program, realize the function of automatically creating 50 user accounts. The account name is STUD1 to STUD50.

#!/bin/bash


I=1


While [$I-le 50]

Do

Useradd stud$i

i=$ ((i+1))

Done


6, the Shell program, to achieve the automatic deletion of 50 user account functions. The account name is STUD1 to STUD50.

[email protected] scripts]# cat test06.sh

#!/bin/bash


I=1


While [$I-le 50]

Do

Userdel stud$i

i=$ ((i+1))

Done


This article is from the "Li Songyang" blog, make sure to keep this source http://lsy666.blog.51cto.com/11729318/1957270

Shell Scripting Exercises

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.