Jump to content

Help with basic bash scipting

Go to solution Solved by Willowwizard,

For bash scripting you can use a cheatsheet (for example, I use this one) to learn about the programmatic elements (e.g. program flow elements like if, for, while, etc.).

and as for other functions like copying (cp) and moving (mv), as tikker said, you can use the man command to view it's documentation.

Here's an example for the first script, anyway:

Spoiler

#!/usr/bin/bash

if [ ! -f test ]; then # If file "test" does not exist, then
  echo "" > test # Create (overwrite if it exists) file "test" with blank contents
else # If file "test" does exist then
  echo "" > test2 # Create (overwrite if it exists) file "test2" with blank contents
fi # End of if statement

cp test temp # Copy file "test" into "temp" (overwriting if it exists)
mv test ./storage/.conf # Move "test" into storage/.conf/ (storage/.conf must exist beforehand)
mv temp ./storage/.conf # Move "temp" into storage/.conf/ (storage/.conf must exist beforehand)

 

Edit: You can learn about user/group/password management here

I need some help with my school work. I have been slacking off too much and am now neck deep in s**t. I have until Friday 6.4.2018 to create three separate scripts for school.

 

They go as follows:

Script 1:

Write a script that will create a file "test", if it doesn't exist yet. If the file exists, create "test2" (if test2 already exists just overwrite it). Then copy the "test" into the file "temp" (overwrite the existing "temp" if it exists) and move both files inside the "storage/.conf". All operations are relative to the current location where we reside when we run the script. After the execution of the script is completed we need to be in the same location as in the beginning.

 

Script 2:

Write a script that creates the user "Rudolf Eror" with the username "banana". Password should be given to the script as the first argument and must not be empty. While creating the user, encrypt the password using the function sha-512. If the user was successfully created output "User created" with the exit status 0, else the exit status should be 42

 

Script 3:

Write a script that accepts two parameters. From those parameters, the script returns a random number between 1 and the first and between 1 and the second parameter. The script then writes out this as "An item has been selected from row X in column Y" where X and Y represent a random number.  If the user doesn't supply two arguments or provides invalid values (0 or lower) the script should output "Input correct values/arguments". However, we assume, that with two arguments supplied, the user will always provide two numbers.

 

For example:

input: ./script.sh 2 3 

output: An item has been selected from row 2 in column 1

 

 

 

I know I should study beforehand but who here can claim that no school project has ever snuck up on them.

Thank you for your help and if you have any educational sites where I could learn these things please post them here :)

Link to comment
Share on other sites

Link to post
Share on other sites

Most of the things you listed have simple linux commands associated with them, so look up how to do task X in linux. Then mold some Bash around it, as you can just use these commands in your script, et voila.

If your not sure what a command does / can do you can use "man <command>" to see the documentation.

 

Also start simple, don't try to write the entire thing at once, but rather implement its functionality incrementally.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

For bash scripting you can use a cheatsheet (for example, I use this one) to learn about the programmatic elements (e.g. program flow elements like if, for, while, etc.).

and as for other functions like copying (cp) and moving (mv), as tikker said, you can use the man command to view it's documentation.

Here's an example for the first script, anyway:

Spoiler

#!/usr/bin/bash

if [ ! -f test ]; then # If file "test" does not exist, then
  echo "" > test # Create (overwrite if it exists) file "test" with blank contents
else # If file "test" does exist then
  echo "" > test2 # Create (overwrite if it exists) file "test2" with blank contents
fi # End of if statement

cp test temp # Copy file "test" into "temp" (overwriting if it exists)
mv test ./storage/.conf # Move "test" into storage/.conf/ (storage/.conf must exist beforehand)
mv temp ./storage/.conf # Move "temp" into storage/.conf/ (storage/.conf must exist beforehand)

 

Edit: You can learn about user/group/password management here

Desktop: HP Z220 Workstation, 12 GB RAM, 2x500 GB HDD RAID0, + GTX 1060 3GB

Laptop: ThinkPad T430, 8 GB RAM, 1x120 GB SSD

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, TakataruMC said:

For bash scripting you can use a cheatsheet (for example, I use this one) to learn about the programmatic elements (e.g. program flow elements like if, for, while, etc.).

and as for other functions like copying (cp) and moving (mv), as tikker said, you can use the man command to view it's documentation.

Here's an example for the first script, anyway:

  Reveal hidden contents


#!/usr/bin/bash

if [ ! -f test ]; then # If file "test" does not exist, then
  echo "" > test # Create (overwrite if it exists) file "test" with blank contents
else # If file "test" does exist then
  echo "" > test2 # Create (overwrite if it exists) file "test2" with blank contents
fi # End of if statement

cp test temp # Copy file "test" into "temp" (overwriting if it exists)
mv test ./storage/.conf # Move "test" into storage/.conf/ (storage/.conf must exist beforehand)
mv temp ./storage/.conf # Move "temp" into storage/.conf/ (storage/.conf must exist beforehand)

 

 

You Sir, are a lifesaver!

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, RageAx said:

You Sir, are a lifesaver!

No problem, always glad to help, you can ask me if you have any more questions.
Oh, and if you were wondering, the #!/usr/bin/bash line in the script is necessary since it allows the script to be run like ./script.sh , without it you would need to run it like $ bash script.sh . Also actually the proper way would be to do #!/usr/bin/env bash as bash may not be always located in /usr/bin/bash

Desktop: HP Z220 Workstation, 12 GB RAM, 2x500 GB HDD RAID0, + GTX 1060 3GB

Laptop: ThinkPad T430, 8 GB RAM, 1x120 GB SSD

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

×