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 09 '22

Bash script to connect two drives

Thumbnail self.rclone
2 Upvotes

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 • • 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 • • 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 08 '20

Editing the PS1 variable

2 Upvotes

This is the question I have:

This sounds like I have to use an if statement but I don't know how that works, what do I do?


r/bashscripts • • Oct 27 '17

Nested if statements

2 Upvotes

Can someone help me out. Not sure why the nested if are not hitting even though the files exist

if [ ! -d node_modules ]; then
    echo "node modules@@@@@@"
    if [ -f yarn.lock ]; then
        echo "We have Yarn"
        yarn install
    elif [ -f package-lock.json ]; then
      echo "we have package lock"
      npm install
    else
      yarn install
    fi
fi

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 • • Jan 01 '23

Bibtex files

1 Upvotes

Could you guys help me with my task for the uni? I should to write a bash script, that helps one to serach for the author etc.

I need a command line that helps me do that

here is an example for bibtex


r/bashscripts • • Nov 22 '22

Bash interaction with web frontend

1 Upvotes

Long story short, because of a weird quirk with my ISP I want to restart my router automatically every night. I have a Linux server on my lan, so ideally, I would simply have my server run a script that does this for me.

In order to restart my router manually, I need to login to my router, press, a button in the top left corner, and then confirm my choice. That's it. I assume there are some Linux tools that can already do something like this, but I'm uncertain where to start.

The log in page seems like the big hurdle here. Otherwise i guess it could be done with a curl oneliner.


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 • • 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 • • 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 • • 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 • • 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 • • 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 • • Dec 24 '20

How to grep a variable

1 Upvotes

Hello guys i am new in bash scripting. Currently I am writing a script to automating docker container. It has three functions kill all docker container, start a new container on a port and show the link(eg. http://127.0.0.1:333 etc.).to run all these i need to check whether a port is listening or not. To do that I first used sudo lsof command with -i:$PORT but it didnt worked so next i used netstat -tulpn with grep $PORT it also didn't worked. it shows some error on grep. Can somebody please help me on this. How can I check a port from a variable whether listening or not. And one more thing the command should be like this ./docker-test 3000 Or ./docker-test kill to kill all running container. Somebody plzz help


r/bashscripts • • Aug 30 '20

hello "if i see a specified msg on terminal , then i need to do a task"

1 Upvotes

hello .. i am trying to write a script and i am stuck at one point .Here is the scenario .. i need to mount an iso and trigger a installation .. after a later point of time , it prompts in the terminal to mount ISO2.

Now i have written the script to mount iso1 and trigger the installation , but how can i include the part " that if i see "msg" on terminal , mount ISO2

Need urgent help .. Thanks a lot in advance!


r/bashscripts • • Aug 28 '20

play midi from a tar.gz file

1 Upvotes

Hey all,

So I have a very odd question. Is it possible to play media from a tar.gz file? In this case I got about 30K+ midi files compressed and I was just wondering if playing these files can be done without being extracted.

The closest I got is using the following line to find the files inside the tar.

tar -xvf midi.tar.gz --to-command "find . -type f -name "*.mid" | wildmidi"

But all that does is state the following error

ERROR: No midi file given
Usage: wildmidi [options] filename.mid
tar: 2514: Child returned status 1
midi/piano/Wonderboy_3_Melody_Piano.mid

So basically it won't pipe to wildmidi (the player I use) even though it finds the files.

Also vim is not an option in this case to browse the files.

Yes, I know, a very odd question and I am doubtful there is a solution, but if there is, it would be awesome. ^_^


r/bashscripts • • Aug 19 '20

XXD from multiple files of the same extension

1 Upvotes

Hey all

So I have a question in regards to a useless, but fun, project.

The script so far

#!/bin/bash

d2s=$(find $HOME/test -type f -name "*.d2s")
charname="$(xxd -u -p -l16 -s 20 assone.d2s | tr '0' 'n'| xxd -r -p)"
charclass="$(xxd -u -p -l1 -s 40 "$d2s")"
echo "Character Name: $charname"
echo "Character Class: $charclass"

What I am doing with it:

I have a bunch of files, in this case .d2s files (save games). In it is data stored like Character Name, Char Class, level, etcetera. So what this does is extract the data so I can show this in a simple bash script and from there ... well do what I can do in the game as well. :P This part is actually quite easy as long as I pay proper attention.

What is working:

If I extract the data from a file directly named in the lines (charname/charclass) it works and it shows me the output. So far I get the char name and the char class and I can print this out.

What I want:

Use the find command to, for now, find the .d2s characters (could be more, could be less) inside my test folder. (I choose $HOME/test as I already have backups and just want to focus on this directory, the end result would be different in terms of finding)

What the error shows when I run the script when I run it with the "$d2s" variable:

xxd: /home/charlie/test/amaone.d2s
/home/charlie/test/necone.d2s
/home/charlie/test/assone.d2s: No such file or directory
Character Name: assone
Character Class:

In other words, the class won't show up, yet the files are found and listed correctly.

Is there a way how I can just read the output of all the files?


r/bashscripts • • Jul 10 '20

what am i missing? auto-start cron script

1 Upvotes

so my bash scripting is terrible apparently what am i doing wrong
my makerun variable is not being handled properly

#!/bin/bash
#INFO: this script should be run with cron or manually .
displaystart='Xvfb :99' & #required to start deusex otherwise wine crashes (starts virtual display on port :99)
displayexport='export DISPLAY=:99' & #export for a virtual display on port :99 for our current cli session
process='Deusex.exe' #proccess that the script is looking for every couple seconds
makerun=/usr/bin/wine "/home/deusex/.wine/drive_c/Program Files/dxmp-cdx/System/DeusEx.exe DXMP_PoolParty.dx??Mutator=DXMapVote100.MVMutator -server" #command that we are running if not found running already

if ps ax | grep -v grep | grep $process > /dev/null
then
    exit
else
    $displaystart &
    $displayexport &
    "$makerun" &
    echo "SCRIPT REBOOTED GAME SERVER AS IT APPEARS IT HAS CRASHED ¯_(ツ)_/¯  Time of fuck up: $(date)" >> /home/deusex/crashlog.log
fi

exit

thanks for any help


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 • • Nov 22 '19

Help with Cron

1 Upvotes

So I have a very simple script that I would like to run.

#!/bin/bash

sudo apt update -y

sudo apt full-upgrade -y

reboot echo "Update successful : $(date)" >> /tmp/update/log

I am trying to run this with cron, which looks like this

* * * * * bash /home/me/Documents/Scripts/update.sh

I cannot get this to run and am not quite sure where I am going wrong. Any assistance would be appreciated.


r/bashscripts • • Nov 07 '19

Need help with scripting

1 Upvotes

I have this script that I am working on and need help.

I don't know what to do I check it over several times and need someone else to look it over

#!/bin/bash

echo "beginning storage check..."

exec >> ~/storage_report.txt

echo "Date : $(date)"

echo "------------------"

part=$(df -h | awk '{print $1}' | grep '/dev')

for z in ${part[*]}

do

checkper=$(df -h | grep $z | awk '{print $5}' | cut -d '%' -f1)
if \[ $checkper -ge 95 \] && \[ $checkper -le 100 \]; then

		echo "$z is $checkper% full."

	else

		echo "ALERT: $z is $checkper$ full! recommended immediate action!"

	elif \[\[ $checkper -ge 95 \]\] && \[\[ $checkper -le 100 \]\]; then

echo "CAUTION: $z is $checkper% full! Consider freeing up some space."

		elif \[\[ checkper -lt 50 \]\]; then

		echo "$z is $checkper% full. No action needed."

	else

		echo "Encountered an error. status code: $?" >&2

		exit $?

fi

done

echo "storage check complete. Report saved to storage_report.txt" >&2


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.