Jump to content

Need some assistance for Linux shell scripting

D13H4RD

Hello. I'm currently in the midst of doing a Linux terminal shell-script for an assignment. The assignment requires that we create a shell script which backs up all files with a certain extension throughout the Linux system. Basically, if we enter ".txt", all files with that extension will be backed up. The requirements are as follows;

 

  • The script should first ask a user if they'd like to load a saved preset. If yes, it prompts which preset to load, in which it executes from there.
  • If the user decides to not load a preset, the script then asks for the desired filetype extension that is to be backed up, along with the desired destination path.
  • The inputs for above should also be saved into a preset file so that it can be quickly accessed in the future. This should be named by the user.
  • The backup process should also be logged into a log.txt file.

This is what I've got so far;

code.png.b4f72efcc56b94748df1b3be4ac52917.png

#!/bin/bash

function introduction(){
	echo "Welcome to this totally simplified backup utility!"
    echo "Please specify if you would like to use a pre-determined setting."
    read setpreference
    if [ "$setpreference" == "Yes"] || [ "$setpreference" == "yes" ]; then
    	echo "Entered the desired preference file"
        read preffile
	elif [ "$setpreference" == "No" ] || [ "$setpreference" == "no" ]; then
    	main
	fi
}

function main(){
	echo "Enter the desired filetype you would like to backup"
    read filetype
    echo "$filetype" > config.txt
    backuptime='date +%b-%d-%y'
    echo "Enter the path and directory of the desired destination"
    read destination
    echo "$destination" >> config.txt
    
    tar -cpzf $destination
    
    echo "Backup successful"
}

Sorry to all the seasoned Linux users for the hodgepodge. But I'm totally new to this sort of thing and I really do need help.

 

There's also a second part of the assignment which involves creating another script to evaluate the functionality of the first but I'll explain that later.

The Workhorse (AMD-powered custom desktop)

CPU: AMD Ryzen 7 3700X | GPU: MSI X Trio GeForce RTX 2070S | RAM: XPG Spectrix D60G 32GB DDR4-3200 | Storage: 512GB XPG SX8200P + 2TB 7200RPM Seagate Barracuda Compute | OS: Microsoft Windows 10 Pro

 

The Portable Workstation (Apple MacBook Pro 16" 2021)

SoC: Apple M1 Max (8+2 core CPU w/ 32-core GPU) | RAM: 32GB unified LPDDR5 | Storage: 1TB PCIe Gen4 SSD | OS: macOS Monterey

 

The Communicator (Apple iPhone 13 Pro)

SoC: Apple A15 Bionic | RAM: 6GB LPDDR4X | Storage: 128GB internal w/ NVMe controller | Display: 6.1" 2532x1170 "Super Retina XDR" OLED with VRR at up to 120Hz | OS: iOS 15.1

Link to comment
Share on other sites

Link to post
Share on other sites

What exactly do you need help with?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sauron said:

What exactly do you need help with?

Mainly getting the script to recognize user inputs to place into a configuration file that is named by the user, which can then be loaded the next time the script is used so that parameters such as file type and save location need not be re-entered manually 

 

The actual backup portion should be straightforward. It's that part that trips me up. 

The Workhorse (AMD-powered custom desktop)

CPU: AMD Ryzen 7 3700X | GPU: MSI X Trio GeForce RTX 2070S | RAM: XPG Spectrix D60G 32GB DDR4-3200 | Storage: 512GB XPG SX8200P + 2TB 7200RPM Seagate Barracuda Compute | OS: Microsoft Windows 10 Pro

 

The Portable Workstation (Apple MacBook Pro 16" 2021)

SoC: Apple M1 Max (8+2 core CPU w/ 32-core GPU) | RAM: 32GB unified LPDDR5 | Storage: 1TB PCIe Gen4 SSD | OS: macOS Monterey

 

The Communicator (Apple iPhone 13 Pro)

SoC: Apple A15 Bionic | RAM: 6GB LPDDR4X | Storage: 128GB internal w/ NVMe controller | Display: 6.1" 2532x1170 "Super Retina XDR" OLED with VRR at up to 120Hz | OS: iOS 15.1

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Dat Guy said:

Hint: You should not assume that there is a bash at /bin/bash.

Just learned about that. Thanks!

The Workhorse (AMD-powered custom desktop)

CPU: AMD Ryzen 7 3700X | GPU: MSI X Trio GeForce RTX 2070S | RAM: XPG Spectrix D60G 32GB DDR4-3200 | Storage: 512GB XPG SX8200P + 2TB 7200RPM Seagate Barracuda Compute | OS: Microsoft Windows 10 Pro

 

The Portable Workstation (Apple MacBook Pro 16" 2021)

SoC: Apple M1 Max (8+2 core CPU w/ 32-core GPU) | RAM: 32GB unified LPDDR5 | Storage: 1TB PCIe Gen4 SSD | OS: macOS Monterey

 

The Communicator (Apple iPhone 13 Pro)

SoC: Apple A15 Bionic | RAM: 6GB LPDDR4X | Storage: 128GB internal w/ NVMe controller | Display: 6.1" 2532x1170 "Super Retina XDR" OLED with VRR at up to 120Hz | OS: iOS 15.1

Link to comment
Share on other sites

Link to post
Share on other sites

You can get user arguments using the $1 variable. 

 

Then use something like cp *.txt /backupDir to just copy all the files there. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, D13H4RD said:

Just learned about that. Thanks!

/bin/sh should be on every system though

4 hours ago, D13H4RD said:

Mainly getting the script to recognize user inputs to place into a configuration file that is named by the user, which can then be loaded the next time the script is used so that parameters such as file type and save location need not be re-entered manually 

 

The actual backup portion should be straightforward. It's that part that trips me up. 

You could use grep

 

Appending a value to a file is as easy as

echo "Key Value" >> file

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Sauron said:

 


echo "Key Value" >> file

 

Much like 

 

“$destination” >> config.txt?

The Workhorse (AMD-powered custom desktop)

CPU: AMD Ryzen 7 3700X | GPU: MSI X Trio GeForce RTX 2070S | RAM: XPG Spectrix D60G 32GB DDR4-3200 | Storage: 512GB XPG SX8200P + 2TB 7200RPM Seagate Barracuda Compute | OS: Microsoft Windows 10 Pro

 

The Portable Workstation (Apple MacBook Pro 16" 2021)

SoC: Apple M1 Max (8+2 core CPU w/ 32-core GPU) | RAM: 32GB unified LPDDR5 | Storage: 1TB PCIe Gen4 SSD | OS: macOS Monterey

 

The Communicator (Apple iPhone 13 Pro)

SoC: Apple A15 Bionic | RAM: 6GB LPDDR4X | Storage: 128GB internal w/ NVMe controller | Display: 6.1" 2532x1170 "Super Retina XDR" OLED with VRR at up to 120Hz | OS: iOS 15.1

Link to comment
Share on other sites

Link to post
Share on other sites

41 minutes ago, D13H4RD said:

Much like 

 

“$destination” >> config.txt?

I'm pretty sure you need echo. Also you should add a key, otherwise you won't be able to retrieve the value later

echo "Destination $destination" >> config.txt

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Sauron said:

I'm pretty sure you need echo. Also you should add a key, otherwise you won't be able to retrieve the value later


echo "Destination $destination" >> config.txt

 

Didn't know about the key. Thanks.

The Workhorse (AMD-powered custom desktop)

CPU: AMD Ryzen 7 3700X | GPU: MSI X Trio GeForce RTX 2070S | RAM: XPG Spectrix D60G 32GB DDR4-3200 | Storage: 512GB XPG SX8200P + 2TB 7200RPM Seagate Barracuda Compute | OS: Microsoft Windows 10 Pro

 

The Portable Workstation (Apple MacBook Pro 16" 2021)

SoC: Apple M1 Max (8+2 core CPU w/ 32-core GPU) | RAM: 32GB unified LPDDR5 | Storage: 1TB PCIe Gen4 SSD | OS: macOS Monterey

 

The Communicator (Apple iPhone 13 Pro)

SoC: Apple A15 Bionic | RAM: 6GB LPDDR4X | Storage: 128GB internal w/ NVMe controller | Display: 6.1" 2532x1170 "Super Retina XDR" OLED with VRR at up to 120Hz | OS: iOS 15.1

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×