r/bashscripts • • Jan 10 '23

Help me with a wget command

0 Upvotes

I am trying to download content from a website. http://www.tfd.chalmers.se/~hani/kurser/OS_CFD_2020.

The website has content from previous years (2021, 2020, 2019 ...). I want to particularly download the content from 2020. The url that I have pasted above takes me to the 2020 content. But when I use wget with the recursive flag, it is starting to download the wesbite content from 2021.

Is is because of the way the website is structured? Is there any particular way to just download content from 2020?

Many thanks for helping. I am new to linux commands, and I thought this would be a good challenge to begin with.

EDIT:
I am trying to see if I can I use --accept-regex option of wget, but I am not able to make it work.


r/bashscripts • • Oct 31 '20

Bash scripting in Linux

0 Upvotes

Hi there, I need help doing the following bash scripting in Linux:

1- Implement the print_usage function, which will print out the following message: Usage: myuseradd.sh -a <login> <passwd> <shell> - add a user account myuseradd.sh -d <login> - remove a user account myuseradd.sh -h - display this usage message

2- Implement the parse_command_options function which should: Parse the command arguments and call the appropriate function -h should call print_usage -d should call delete_user -a should call add_user If an invalid argument is given, such as -z, print out an error message and the usage information in the print_usage function. ERROR: Invalid option: -z


r/bashscripts • • Nov 27 '22

What does this script do

2 Upvotes

I need to explain what this script does for a question on my classwork but my teacher isn't satisfied with my answer. Can anyone provide some insight into what exactly is happening with this script?

#!/bin/bash

if [ -z “$1” -o -r “$2” ]; then

echo “Usage: myscript [file] [size]”

exit 255

fi

SIZE=`ls -l $1 | awk ‘{print $5}’`

if [ $2 -gt $SIZE ]; then

echo “Your size must be smaller than the file size!”

exit 254

fi

CHUNK=$2

TOTAL=0

PASS=0

while [ $TOTAL -lt $SIZE ]; do

PASS=$((PASS + 1))

echo “Creating $1.$PASS …”

dd conv=noerror if=$1 of=$1.$PASS bs=$CHUNK skip=$((PASS – 1)) \

count=1 2>/dev/null

TOTAL=$((TOTAL + CHUNK))

done

echo “Created $PASS files out of $1.”


r/bashscripts • • Jan 28 '21

[Blog] How to trigger an action at the end of the Shell/Bash script

Thumbnail
self.devops
2 Upvotes

r/bashscripts • • Mar 29 '21

What’s the error in this code ?

Post image
4 Upvotes

r/bashscripts • • Dec 26 '20

I am learning Bash and I will code your scripts for free.

7 Upvotes

I am learning to code in Bash and I already have several scripts done that work perfectly. I have run out of ideas of what to do and the book examples bore me so if you propose me something that you need I will code it for you with pleasure for free.

Only a few conditions:

- Make a small description here in the forum. I will answer you if I accept it or not. I will give priority based on my skills and preferences, not on the order of arrival.

- Only small and not too complex scripts. I prefer to help little to many than much to one. I will also try to do different things if possible.

- Specification and details best by private or email. Please take your time to make it as detailed as possible so that I don´t have to ask around.

- If you are in a hurry, it is better to find another way. I have a job and will code it in my free time.

- Keep in mind that I am not an expert and there is a possibility that after some time I will be unable to make it work properly. In that case you will have to find someone better than me to do the job.

- I will take my time to code and test it carefully, but I do not admit any responsibility if the script damages your files or systems. You should set up a secure test environment to test it.

That's all.


r/bashscripts • • Mar 27 '16

Makefile script using "time" not outputting correctly

2 Upvotes

Im using bash to measure execution time of a few code samples in different languages good output looks like this

bull3t3-:~/****/a3/cobol$ time ./rpn rpn4.txt
real	0m9.782s
user	0m9.789s
sys	0m0.000s

But when I run the makefile to run several commands and output the time, one execution of the command looks like this

time ./rpn rpn1.txt
0.02user 0.00system 0:00.03elapsed 96%CPU (0avgtext+0avgdata     1520maxresident)k
0inputs+0outputs (0major+449minor)pagefaults 0swaps

Here is the makefile in question

default:
	cobc -O2 -m -free rpnConv.cob 
	cobc -O2 -x -free rpn.cob
	-mv ../data/rpn1.txt rpn1.txt
	-mv ../data/rpn2.txt rpn2.txt
	-mv ../data/rpn3.txt rpn3.txt
	-mv ../data/rpn4.txt rpn4.txt
	-mv ../data/rpn5.txt rpn5.txt
	time ./rpn rpn1.txt
	time ./rpn rpn2.txt
	time ./rpn rpn3.txt
	time ./rpn rpn4.txt
	time ./rpn rpn5.txt
	-mv rpn1.txt ../data/rpn1.txt
	-mv rpn2.txt ../data/rpn2.txt
	-mv rpn3.txt ../data/rpn3.txt
	-mv rpn4.txt ../data/rpn4.txt
	-mv rpn5.txt ../data/rpn5.txt
    clean:
    	-rm rpn
    	-rm *.so

r/bashscripts • • Dec 30 '20

Some ticks/tips for Bash script - HTTP request with a Bash native function -- Get IP Address without ip/ifconfig

5 Upvotes

Hello there,

A few weeks ago I started my blog which I'll focus on Tips and Tricks. I wrote only two posts until now, soon I'll bring more. The idea is to bring some stuff that I use/know on a daily basis and it may be useful for others too. I'm trying to keep as simple as possible with some practical examples (+ external links that could explain in-deep, in case someone wants to know more :) ).

Any feedback is more than welcome :)

How to send a TCP request with Bash native feature (e.g.: you can do test an HTTP endpoint without installing cURL / Wget / etc)
https://medium.com/bash-tips-and-tricks/part01-tcp-udp-request-with-a-native-bash-feature-and-without-curl-wget-9dcef59c30aa

How to get the Linux IP address without ip/ifconfig
https://medium.com/bash-tips-and-tricks/getting-the-linux-ip-address-without-any-package-ifconfig-ip-address-etc-7b1363077964


r/bashscripts • • Mar 25 '21

How to code this? I need help

1 Upvotes

How to write a script that can be run from any directory it is placed within user’s Linux directory structure?


r/bashscripts • • Apr 09 '21

Mediainfo NFO file function

1 Upvotes

Hardly a script, but I was hoping to make an alias of sorts on my osmc install/raspberry pi for mediainfo to create the nfo file. In essence I just want to pass in the arguments for the file name as per the below:

mediainfo --LogFile=nfo_file_name.nfo movie.avi

I have the below which cuts off the file extension, I tested it with echo to confirm the correct output but it doesn't work when applied to mediainfo, just wondered if you can tell why.

Test:
function nfo () { /bin/echo ${1::-3}nfo "$1" ;}

mediainfo

function nfo () { /usr/bin/mediainfo ${1::-3}nfo "$1" ;}

The outputs:

Test Function-
$ nfo 12.Angry.Men.1957.720p.mp4

12.Angry.Men.1957.720p.nfo 12.Angry.Men.1957.720p.mp4

Which should feed the correct arguments as strings to the media info program.

When I try with the mediainfo version I get the following:

nfo 12.Angry.Men.1957.720p.mp4

General

Complete name : 12.Angry.Men.1957.720p.mp4

Format : MPEG-4

Format profile : Base Media

... <Rest of the NFO output> But no nfo file is created.

I was thinking I could redirect the output to a new file called $1.nfo but when run "manually" the nfo file is created. I just can't figure out why not for the above.


r/bashscripts • • Apr 07 '21

Help with shell script challenge

2 Upvotes

Hello Shell Scripters!

At work, I've been asked to verify if some PowerShell challenges issued to interviewees could be solved in Bash for certain sysadmin candidates.

I've provided solutions for all of them except one. Well, I did provide a solution, but it's a hack, and I'm racking my brain to think about how to solve it properly:

Write a script that, for each number from 1 to 100, prints a comma-delimited list of numbers in descending order from the current number to 1. Each list should be shown on a separate line as seen below.

1
2,1
3,2,1
4,3,2,1
...
100,99,...,1

Example Solution (from 10 to save space). Originally, I did this:

for x in {1..10}; do for i in {$x..1}; do echo -n $i,; done; echo ""; done

But then I noticed the trailing commas and the end of each line. I then submitted this instead (admitting "OK, I cheated shaving off the trailing comma.")

echo "1"; for x in {2..10}; do for i in {$x..2}; do echo -n $i,; done; echo -n "1"; echo ""; done

Example Output

1
2,1
3,2,1
4,3,2,1
5,4,3,2,1
6,5,4,3,2,1
7,6,5,4,3,2,1
8,7,6,5,4,3,2,1
9,8,7,6,5,4,3,2,1
10,9,8,7,6,5,4,3,2,1

My boss laughed and admitted she added a ().trimend(",") in the PS solution.

What's the proper way to shave off the trailing comma in a situation like this? Any ideas?

Note: the solution doesn't have to be a one-liner, but brevity is appreciated. It's not a "programming" test, but it is a way to see how the interviewee approaches a problem.


r/bashscripts • • Apr 09 '21

Creating an "exportable" GUI for MACOS

6 Upvotes

I'm looking for recommendations for solutions to create an exportable GUI for my shell scripts. Exportable = copying to other computers with no additional software on the target computers needed.

This is for MACOS and if I get around using the Apple app store that would be best.


r/bashscripts • • Feb 13 '21

I wrote a small script - variety-loop

1 Upvotes

for people who dont want a separate keybindings just for rarely used variety

just use variety-loop with just 1 key press and will do (next, previous, trash, favorite or quit)

https://pastebin.com/L3ejkcEw

usage example:

$ variety-loop    
Useage: variety -<input>
  --* -> read more than 1 char
  -q  -> --quit  (Also quits from ^D)
  -n  -> --next
  -p  -> --previous
  -t  -> --trash
  -f  -> --favorite

variety -<< WAITS FOR INPUT >>

r/bashscripts • • Mar 02 '21

How to include keyboard interrupt like "ctrl-c" in "if-else" state condition.

1 Upvotes

I'm working on automation in linux and I can't figure out how to detect if someone uses ctrl-c or escape key or when the terminal is forced closed and create condition that pop-up the dialogue and give a warning message or save options. Any example is appreciated.


r/bashscripts • • Mar 18 '21

Can someone please tell me what’s the error in the equation part of the code?

Post image
1 Upvotes

r/bashscripts • • Feb 17 '21

[Blog] A CLI tool that can easily manage more than 281 runtimes, making it easy to keep things updated and/or switching between versions. And it’s open-source :)

2 Upvotes

I just wrote this post. I'm showing an OSS tool that can easily keep things updated on *NIX/Windows and/or easily switch between versions. It's very useful on Bash/Zsh/Fish scripts that you need to switch between versions.

Feedbacks/suggestions/* are always welcome :)

See on Medium: https://medium.com/bash-tips-and-tricks/an-easy-way-to-switch-between-runtime-versions-nodejs-terraform-and-279-53fdfbcb4049?sk=43548418bf394d80e277a39a76070843

See on my Website: https://www.lozanomatheus.com/post/an-easy-way-to-switch-between-runtime-versions-nodejs-terraform-and-279


r/bashscripts • • Feb 12 '21

[Blog] Bash variables — Things that you probably don’t know about it

6 Upvotes

Hey,

Two days ago I wrote this blog exploring the scope of the Bash variables and the types. I'm explaining their differences, how and when to use each one of them. Feel free to give any feedback/suggestion/* :)

See on Medium: https://medium.com/unboxing-the-cloud/bash-variables-things-that-you-probably-dont-know-about-it-8a5470887331?sk=26693ca772a0c54c99d3712303560ed4

See on my Website: https://www.lozanomatheus.com/post/bash-variables-things-that-you-probably-don-t-know-about-it

Sorry, I'm pretty new on Reddit, I accidentally deleted the original post...


r/bashscripts • • Jun 18 '20

Fake access point(with authentication)

1 Upvotes

Would anyone know how to go around making a bash script that will set up a fake access point, One that will tell you the passwords entered and set a custom ssid?


r/bashscripts • • Jun 19 '20

Help with cp command in bash script

4 Upvotes

Hi everyone! I'm having an issue with the cp command to move folders/files to a new disk. When the filename or folder has a \(space) it won't see it as a complete folder name or file and instead takes the first section of the file and then errors on everything else. Can anyone help me with what I'm doing wrong?

#!/bin/bash

###############################################################
#####################      Variables     ######################

### Set the proper source and destination directory location

SOURCE_DIR="/opt/test/old/"
DEST_DIR="/opt/test/new/"

### Set the username and group name to set on copied files
USER='root'
GROUP='root'

###############################################################

########### Do not edit below this untill required  ##########

### Test if source directory exists
### The programm with stop if source not exists

if [ -d ${SOURCE_DIR} ]; then
	echo "Source directory found"
else
	echo "Source directory not found. Please check above variables are set correctly"
	echo "script exited"
	exit 1
fi

### Test if destination directory exists
### The programm will create destination directory if not exists.
### If failed to create directory, the script will terminate 

if [ -d ${DEST_DIR} ]; then 
	echo "Destination directory found, all ok"
else
	echo "Destination directory not found, creating now"
	mkdir -p ${DEST_DIR}
	if [ $? -eq 0 ]; then
		echo "Sucessfully created destination directory."
	else
		echo "Faild to create destination directory. Script exited"
		exit 1
	fi
fi


### Copy all files available on source directory
### After successfully copying file remove it from source directory.

cd ${SOURCE_DIR}

if [ $? -eq 0 ]; then
	for CURRENT_FILE_NAME in `find . -type f`
	do
		cp --parents ${CURRENT_FILE_NAME} ${DEST_DIR}
		if [ $? -eq 0 ]; then
			echo "File ${CURRENT_FILE_NAME} successfully copied."
			rm -f ${CURRENT_FILE_NAME}
		else
			echo "File ${CURRENT_FILE_NAME} failed to copy"
		fi
	done
fi


## Set the permissions after copying files 

sudo chmod 775 -R ${DEST_DIR}
sudo chown ${USER}:${GROUP} -R ${DEST_DIR}


###################  End of Script  ############################

r/bashscripts • • Sep 09 '22

Bash script to connect two drives

Thumbnail self.rclone
2 Upvotes

r/bashscripts • • Sep 14 '22

Script for appending title to 2nd line of other scripts

2 Upvotes

I have a lot of scripts i wrote to quickly ssh into servers. Usually in the format

Eg. filename is: server-script1.sh

cat server-script1.sh
#!/bin/bash

ssh -i key.pem [email protected]

I’m not too familiar with sed and awk. Those are what i came across in my search for a solution.

I want to add the filename as a comment in the second line for each file so that they all come out in this way:

#!/bin/bash
#server-script1.sh
ssh -i key.pem [email protected]
#!/bin/bash
#server-script2.sh
ssh -i key.pem [email protected]

Any suggestions on how to do this? Thanks


r/bashscripts • • Sep 26 '22

Bash script to determine if a streaming is live or off

1 Upvotes

I found this bash script in the ask ubuntu forum and it works, but I would like to know if it can be used or improved to know if a tiktok stream is live or off, using the bash script via terminal (./test.url.sh /url.txt) in the .txt would be the tiktok addresses for example:

https://www.tiktok.com/@xxxxxx/live

https://www.tiktok.com/@cccccc/live

and that in the terminal tell me if it is live or off, is it possible?

#!/bin/bash

if [ $# -eq 0 ]
then
fin=-
else
fin=$1
fi

urlarr=( $(cat $fin | strings | grep '://' | grep -i "\.com\|\.net\|\.edu\|\.org\|[0-9].*\.[0-9].*\.[0-9].*\.[0-9].*" | tr '\n' ' ' ) )

for i in ${urlarr[@]}
do
if wget -q --tries=1 --no-cache --spider -O /dev/null --ignore-length -T 1 "$i"
then
echo $i is good.
else
echo $i is bad.
fi
done

exit $?


r/bashscripts • • Oct 02 '22

Bash script to automate video downloads

3 Upvotes

Looking to make a script to automate video downloads. The process normally goes like this: 1. YouTube-dl 'url-to-vid' (this will download an HTML) 2. Open HTML and search for "data-src" (so far only two lines have returned with this search, one relating to a .mp4 and another .jpg, we need the url for the .mp4) 3. Copy & paste url again with YouTube-dl and then rename the file using the name of the HTML file. 4. rm original HTML file.


r/bashscripts • • Sep 06 '23

I love BASH

9 Upvotes

I’m no scholar but I’ve been writing scripts for a very long time. Just started my blog Follow, if you like

https://bashydata.wordpress.com


r/bashscripts • • Nov 14 '22

Having trouble with this task anyone wanna point me in the right direction?

2 Upvotes

So i need to make a script that transfers a backup from one system to another remote system using ssh, but I am not sure how I will be doing this, can anyone give some tips to help me complete this task?