Bash scripts: start
Bash scripts, part 2: loops
Bash scripts, part 3: command line options and keys
Bash scripts, part 4: input and output
Bash Scripts, Part 5: Signals, Background Tasks, Script Management
Bash scripts, part 6: functions and library development
Bash scripts, part 7: sed and word processing
Bash scripts, part 8: awk data processing language
Bash scripts, part 9: regular expressions
Bash scripts, part 10: practical examples
Bash scripts, part 11: expect and automate interactive utilities
write
command itself, write
, is pretty simple. In order to use it, it is enough to know the username and the name of his terminal. However, in order to successfully send a message, in addition to the actual data about the user and the terminal, you need to know whether the user has logged in and if he has not prohibited entry to his terminal. As a result, several checks need to be performed before sending a message.who
command. It allows you to find out information about users working in the system. In its simplest form, its call looks like this: $ who
mesg
, prohibit sending messages to him. Thus, before trying to send something to someone, it would be nice to check whether sending messages is allowed. If you need to know your own status, just enter the mesg
without parameters: $ mesg
mesg
will print “is n”.who
command that you already know with the -T
switch: $ who -T
$ mesg y
mesg
returns “is y”.write
command. If the reception of messages from the user is allowed, using this command, he can send messages using his name and information about the terminal.write
you can send messages to users logged into the virtual console. Users who work in a graphical environment (KDE, Gnome, Cinnamon, and so on) cannot receive such messages.likegeeks
, initiate a communication session with the user testuser
, which works in the pts/1
terminal, as follows: $ write testuser pts/1
ENTER
, we can enter the next line of the message. After the text entry is completed, you can end the communication session using the CTRL + D
key combination, which allows you to enter the end-of-file character .EOF
, located at the bottom of the terminal window. It indicates the end of the message text. logged_on=$(who | grep -i -m 1 $1 | awk '{print $1}')
who
command are passed to the grep
. The -i
switch of this command allows you to ignore the case of characters. The -m 1
switch is included in the command call in case the user has logged in several times. This command will either not display anything, or will display the user name (we will specify it when the script is called, it will fall into the position variable $1
) corresponding to the first session found. The output of grep
is the awk
. This command, again, either does not print anything, or displays an element written to its own variable $1
, that is, the name of the user. As a result, what happened is in the variable logged_on
.ogged_on
, see if there is anything in it: if [ -z $logged_on ] then echo "$1 is not logged on." echo "Exit" exit fi
if
construct, take a look at this material.senderscript
in the senderscript
file and senderscript
, giving it, as a command line parameter, the user name testuser
.logged_on
variable with zero length. If this is the case, we will be informed that at the moment the user is not logged in and the script will exit using the exit
. Otherwise, the script will continue. allowed=$(who -T | grep -i -m 1 $1 | awk '{print $2}') if [ $allowed != "+" ] then echo "$1 does not allowing messaging." echo "Exit" exit fi
who
command with the -T
switch. In the line of information about the user who can receive messages, there will be a plus sign (+), but if the user cannot receive messages, there will be a dash (-). What happened after calling who
is passed to grep
, and then awk
, forming the variable allowed
.allowed
variable. If the plus sign is not in it, we will inform you that sending messages to the user is prohibited and finish the work. Otherwise, the script will continue. if [ -z $2 ] then echo "No message parameter included." echo "Exit" exit fi
terminal=$(who | grep -i -m 1 $1 | awk '{print $2}')
echo $2 | write $logged_on $terminal
$ ./senderscript testuser welcome
senderscript
script, passing a message consisting of several words: $ ./senderscript likegeeks welcome to shell scripting
shift
command and the while
. shift while [ -n "$1" ] do whole_message=$whole_message' '$1 shift done
$2
, the variable whole_message
: echo $whole_message | write $logged_on $terminal
#!/bin/bash logged_on=$(who | grep -i -m 1 $1 | awk '{print $1}') if [ -z $logged_on ] then echo "$1 is not logged on." echo "Exit" exit fi allowed=$(who -T | grep -i -m 1 $1 | awk '{print $2}') if [ $allowed != "+" ] then echo "$1 does not allowing messaging." echo "Exit" exit fi if [ -z $2 ] then echo "No message parameter included." echo "Exit" exit fi terminal=$(who | grep -i -m 1 $1 | awk '{print $2}') shift while [ -n "$1" ] do whole_message=$whole_message' '$1 shift done echo $whole_message | write $logged_on $terminal
$ ./senderscript likegeeks welcome to shell scripting
du
, which displays information about how much disk space are occupied by files and folders. By default, it displays information only about directories, with the -a
option, individual files are -a
in the report. Its -s
key allows you to display information about the size of directories. This command allows, for example, to find out the amount of disk space that is occupied by the data of a certain user. Here is the call to this command: $ du -s /var/log/
-S
(capital S) key is better suited, since it allows you to get information both on the root folder and on the directories attached to it: $ du -S /var/log/
du
gives out should be sorted using the sort
command: $ du -S /var/log/ | sort -rn
-n
switch tells the team to use numeric sorting, the -r —
switch to reverse the sort order (the largest number will be at the top of the list). The data obtained is quite suitable for our purposes.sed
, which will remove all rows from the resulting list, starting with the eleventh. The next step is to add its number to each received line. It will also help sed
, namely - his team N
: sed '{11,$D; =}' | sed 'N; s/\n/ /' |
awk
. Let's pass awk
what we got after processing the data with sed
, applying, as in other cases, the pipeline, and output the data using the printf
command: awk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}'
$ du -S /var/log/ | sort -rn | sed '{11,$D; =}' | sed 'N; s/\n/ /' | awk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}'
MY_DIRECTORIES
and list the directories of interest to us: MY_DIRECTORIES="/home /var/log"
for
loop and call the above sequence of commands for each element of the list. Here is the result: #!/bin/bash MY_DIRECTORIES="/home /var/log" echo "Top Ten Disk Space Usage" for DIR in $MY_DIRECTORIES do echo "The $DIR Directory:" du -S $DIR 2>/dev/null | sort -rn | sed '{11,$D; =}' | sed 'N; s/\n/ /' | awk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}' done exit
MY_DIRECTORIES
.du
command in this script can be invoked with other keys, the resulting list of objects may well be filtered out, in general, there opens up a wide scope for independent experiments. As a result, instead of working with a list of folders, you can, for example, find the largest files with the .log,
extension .log,
or implement a more complex algorithm for finding the largest (or smallest) files and folders.write
command, or a script that helps in finding files and folders that take up a lot of disk space, but in describing the development process itself. Having mastered these examples, experimenting with them, perhaps - by adding them or completely reworking, you will learn something new, improve your skills in developing bash scripts.Source: https://habr.com/ru/post/328346/
All Articles