Bash This week you will create two bash scripts Create a bas

Bash. This week you will create two bash scripts:

Create a bash script named ‘backup.sh’. This script should create a tar file containing all .c files in the current directory, gzip that tar file, copy the resulting file to the user’s home directory, and report that the file was created. The date of the create gzip file should include the current date and time.

The output of your program should look like this:

$ ls

backup.sh goodbye.c hello.c

$ ./backup.sh

all .c files copied to ~/backup2016-09-21_09-29.tar.gz

$ ls ~/back*

/home/CLASSES/brunsglenn/backup2016-09-21_09-29.tar.gz

$

The file name format should be exactly as shown, with first the year, month, and day, separated by hyphens, then an underscore, then the hour and minute, separated by a hyphen.

Create a bash script named ‘most_proc.sh’. This script should show the top 10 users according to the number of processes they are running. It should show the process count and the user name.

The output of the script should look something like this, depending on which users are active.

$ ./most_proc.sh

157 root

12 apache

11 brun1992

4 bb

2 postfix

2 hart4492

2 68

1 USER

1 sshd

1 rpcuser

I don’t care about the order of users with the same number of processes.

Some hints: 1) you will probably want to use ‘ps’ with certain options to see all processes of all users. 2) if you use “awk ‘{print $2}’ “ you will get the second column of text. 3) don’t forget some of the text processing commands we covered in lecture this week.

Solution

Here is backup.sh

#!/bin/sh

#Please provide the folder name you are using for the variable \"path\"

path=\"test\"
filename=\"backup$(date +\"%Y-%m-%d_%H-%M\").tar.gz\"
cd ~/$path
tar -zcvf ~/$filename *.c
cd ~
zipfiles=$(ls back*)
echo \"Zip files present:\"
echo $zipfiles
if [ zipfiles ]; then
echo \"All .c files copied to \" ~/$filename
else
echo \"Cannot zip the files!!\"
fi

Here is most_proc.sh

#!/bin/sh

#All users according to the number of processes they are running
ps -eo user=|sort|uniq -

Bash. This week you will create two bash scripts: Create a bash script named ‘backup.sh’. This script should create a tar file containing all .c files in the cu
Bash. This week you will create two bash scripts: Create a bash script named ‘backup.sh’. This script should create a tar file containing all .c files in the cu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site