Questions tagged [bash]

BASH is the GNU Bourne Again SHell, the successor to the classic Unix Bourne sh (shell). It's the default shell for various GNU systems.

Filter by
Sorted by
Tagged with
22
votes
4answers
1k views

“Up” script for moving up directories quickly

A long time ago I created a script for moving up directories very quickly in the command line using the command up. You can find usage notes here. It's a very ...
22
votes
7answers
3k views

Extracting the classpath entries from a jar file's manifest

Sometimes I need to search for dependencies in the classpath entries in the manifest of jar files. A jar manifest with classpaths looks like this: ...
18
votes
4answers
1k views

Discover random web servers and print the index page title

Github repo I wrote this little toy today that probes random web servers and prints out the title of their index page. I'm curious if this code is any good since I'm a bit rusty. get_titles.py <...
16
votes
2answers
10k views

To calculate the total CPU usage as a percentage

I have been developing the below method to calculate the CPU usage of a Linux system at a point in time, as a percentage of the total number of cores available in the system. ...
13
votes
3answers
1k views

Was this shell script ready for open-source? (Random XKCD wallpaper)

I wrote the following little shell script and published it on GitHub. In the repo there's also a README file that explains how to use it in more detail. This is the first time I open-sourced/...
13
votes
6answers
780 views

Bash script that lowercases files

I have the following bash script that: Finds all files with .cfc and .cfm extension and converts them to lowercase Stores the ...
13
votes
2answers
186 views

Flac Converter And Organizer

I really like flac files, but my phone can't store them all. So I made a script for converting them, that slowly became a full featured flac organizer. I'd like to improve my programming skills, as ...
12
votes
2answers
1k views

'find' template for handling any file

After growing tired of all the ways in which a file loop can be broken (find -print | while read) or unreadable (find -exec with ...
12
votes
2answers
3k views

Bob Marley is jamming on the wifi

Bob is jamming it's way through the city deauthenticating people from thier wifi for a few seconds. First scanning all possible wifi-AP and then pretty printing them, afterwards you can choose a wifi ...
12
votes
3answers
2k views

Bash script that updates Intel e1000e driver

I am now writing a small bash script that updates an Intel NIC driver to the latest version from the official website. Is there any way to improve\simplify the script? I want to avoid a lot of "if......
12
votes
1answer
101 views

Load SSH key remotely

I often create new accounts or virtual machines, and then have to load my SSH public key/signature in to the authorized keys on that account. That process can be tedious, so I created this script that ...
11
votes
5answers
2k views

Git delete all merged in local branches

I have written a shell script utility for deleting all merged local branches. ...
11
votes
3answers
552 views

Iptables Lockdown

I have nginx serving files with uwsgi and I wanted to lock my server down just to allow SSH and Nginx to run. ...
11
votes
2answers
494 views

Parallelized for loop in Bash

I am using a Bash script to execute a Python script multiple times. In order to speed up the execution, I would like to execute these (independent) processes in parallel. The code below does so: <...
11
votes
1answer
863 views

Securely delete a file

I am trying to write a script to securely delete a file. What I am intending to do is to pad the file with /dev/zero once, and then with /dev/urandom three times and then delete the file. For now the ...
11
votes
2answers
128 views

Bash script to clear files not matching a given extension

Using LaTeX creates a bunch of auxiliary files like foo.aux, foo.log, etc. Sometimes, I want to create a clean run of my ...
11
votes
3answers
221 views

Make a directory and change to it

A basic shell function to create a directory and change to it goes like this: mkcd () { mkdir "$1" && cd "$1"; } This works well in many cases but breaks ...
10
votes
3answers
758 views

Shell Script (Bash) to sum multiples of different arguments below a number

I'm interested in learning how to create shell scripts with bash so here's one of my first exercises, taken from here. It is an extension of the problems Elementary - 4 and 5. The goal of the program ...
10
votes
1answer
340 views

Bash Music Player 2

This post is a follow-up to this. I wanted further reviews since the code I provided became too old when I got replies, which is why I'm providing it again here. I'm hoping I could get reviews for ...
10
votes
2answers
889 views

JSON Parsing in Bash

I have a json file which needs to be restructured. The following is the code. ...
10
votes
4answers
8k views

Unix shell function for adding directories to PATH

Adding a directory to the PATH in Unix/Linux is a very common task. However, what if the directory is already in the path? My goal is to write a shell function (...
10
votes
1answer
2k views

Bash script to simplify Git deployment workflow

In my company I follow the following workflow. (I think it can be defined a kind of 'Continuous Integration'.) Workflow: We have 3 fixed branches (staging/...
10
votes
1answer
952 views

Calculating pi using bash

I've written a little program to calculate pi using the Nilakantha series: For this formula, take three and start alternating between adding and subtracting fractions with numerators of 4 and ...
10
votes
3answers
19k views

Extracting data from text file in bash using awk, grep, head and tail

I've been writing bash script on and off, with pretty good results in terms of getting the job done. However, I'm worried that my script might be very ugly, as I am a beginner. I'm looking for advice ...
10
votes
1answer
460 views

Bash command helper in C

Previously, I had written this program in C++ and asked about it here on Code Review. After spending some time with C++, I desired to become more familiar with its namesake. This is my first C program....
10
votes
2answers
154 views

Add license to beginning of files

I'm trying to come up with a script that will add license headers to certain files (depending on extension). My Bash is very average, so any guidance would be much appreciated. I'm not quite sure ...
10
votes
1answer
110 views

Locating matching files with input folder and file prefix

This is my first more-than-1-line script. It takes an input folder and a file prefix and gets all the matching files. For the first of the files, the script grabs the first line and appends an extra ...
10
votes
1answer
254 views

Mass build and save of docker images

I would welcome feedback about this script to automate the building of docker images from a set of dockerfiles. The dockerfiles are provided to the script via stdin. The images are saved to the ...
10
votes
1answer
94 views

Automating a WordPress install

I do not have a whole lot to do over winter break, so I wrote this little script to automate a Wordpress install (currently can only install once instance) on a fresh Debian server (tested, working ...
10
votes
1answer
114 views

POSIX shell compatible and POSIX conforming dirname implementation

The discussion in the comments of this answer made me wonder how hard it would be to write a built-in only version of dirname. This is what I ended up with but I'm ...
9
votes
5answers
169 views

Disallow `rm` to remove `*`

Note: If you googled this by the title of this question, don't use this script unless you know what it is supposed to do. This is a script in bash 3+ that I have ...
9
votes
2answers
546 views

Bash script to truncate subject line of incoming email

I'm going to put this script into production in a mail server /etc/aliases file: we have a system that receives email but the subject line must be limited to a certain size. The proposed usage in the ...
9
votes
2answers
718 views

Fizz having an argument with Buzz

For my current project I need to validate responses. The requests will be send from multiple different shell scripts and should be controlled from another script. I'd never targeted a ...
9
votes
2answers
785 views

Count line of code for a JavaScript project

Here is my bash script that I just wrote to count line of code for JavaScript project. It will list number of: Comment lines Blank lines All lines Here is my script: ...
9
votes
2answers
5k views

Using /proc/stat to calculate CPU usage

Inspired by this question: To calculate the total CPU usage as a percentage I wrote the following bash script (as an answer) to calculate the CPU usage for a (configurable) period over all CPU cores ...
9
votes
2answers
1k views

Memoizing or caching Bash function results

Update: I've implemented several of the features suggested below and packaged the improved code into a dedicated project: bash-cache . I display a number of expensive operations in my Bash prompt (e....
8
votes
3answers
136 views

Bash script to batch rename files

This is my homebrew bash script for batch renaming files extensions (my music library needs organizing): ...
8
votes
1answer
779 views

Create New C++ Class

I'm currently developing a new version of ExpressGenGen Using TDD. While I'm rewriting the new version of my Code Generator Generator. I'm using its old version to generate code to be used by the new ...
8
votes
3answers
406 views

PS1 for Bash prompt showing last exit status, written mostly in Python

I had most of this functionality accomplished in BASH, but I was having trouble getting the last process's return value (even though I had a minimal working example). Plus, the escape codes made ...
8
votes
3answers
388 views

Update MediaWiki settings with dynamic IP address

I'm running a MediaWiki instance from a home server where the public IP address is not guaranteed to remain static. The following script is meant to run at boot time to check if the IP address has ...
8
votes
4answers
193 views

Project Euler 1 - Multiples of 3 and 5 by Loki

Multiples of 3 and 5 Multiples of 3 and 5 Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. ...
8
votes
1answer
893 views

Guessing the JDK home directory

My project is using a specific version of JDK which is jdk-6 and the default JDK_HOME path will vary from distribution to distribution, so I wrote a script to guess it. Is there any way to do this ...
8
votes
2answers
301 views

HTML Book Compiler

I have written a BASH script to parse and compile HTML code into a single page. The script works as expected, although my code is not completely re-usable and the standard output messages could be ...
8
votes
1answer
734 views

Bash script - automate an upload image + description process on a server

I was tasked at work to improve a system where through a web interface a client, which eventually became us, uploads a bunch of images with descriptions (latter coming from csv files), the images are ...
8
votes
3answers
467 views

Can I speed up this simple versioning/backup script?

I'm writing a simple automatic backup/versioning bash script. It should basically mirror a directory structure somewhere, and then copy files over there when they've been altered. The ghost files ...
8
votes
2answers
174 views

Copy files in parallel by reading what files to copy from few other files

I am working on a little complicated shell script for the first time and this is what it should do: During startup, it figures out what is my clientid by looking ...
8
votes
2answers
163 views

Identify users who have logged in as root

This is a program written in Python 3.x and Bash to check and find any users who have logged in as root via sudo su, sudo -i, ...
8
votes
1answer
247 views

Script for handling PPA's on Ubuntu

I'm writing a bash script to handle the PPA's on Ubuntu & derivatives. I was wondering how to know if I'm handling this the right way. So, the script works (flawlessly), but I posted it to a ...
8
votes
1answer
857 views

Bash script to clone all public repos or gists for a specified user, optionally to a directory of choice

I wrote this script as a way to backup and/or download quickly a set of repos or gists from a user. I don't have any major concerns but I'm a noob at bash scripting, and I've been scouring the ...
8
votes
1answer
1k views

Connecting to EC2 instances using the instance name instead of IP

I'm an intensive user of AWS EC2 instances, many instances are launched, stopped, repurposed, etc. To connect to any instance using SSH I must keep track of their ...