bash

Help with shell script - Rename filenames (remove special chars) in a directory?

I have a directory of csv files with spaces and all kinds of characters. How do I rename them? The following gives an error. #! /bin/bash cd DirectoryName for file in *.csv; do #echo $file filename=${file%.*} file_clean=${filename//[ ()$+&\.\-\'\,]/_} final= "$file_clean.csv" mv "$file" $final done cd .. Thanks!...

Passing the shell to a child before aborting

Current scenario, I launch a process that forks, and after a while it aborts(). The thing is that both the fork and the original process print to the shell, but after the original one dies, the shell "returns" to the prompt. I'd like to avoid the shell returning to the prompt and keep as if the process didn't die, having the child handle...

cURL - scanning a website's source

Hello everyone, I was trying to use the program cURL inside of BASH to download a webpage's source code. I am having difficulty when trying to download page's code when the page is using more complex encoding than simple HTML. For example I am trying to view the following page's source code with the following command: curl "http://s...

Continue to grep for traceroute result with bash

Every night I go through the same process of checking failover systems for our T1's. I essentially go through the following process: Start the failover process. traceroute $server; Once I see it's failed over, I verify that connections work by SSHing into a server. ssh $server; Then once I see it works, I take it off of failover. ...

sed variable replacement

I have tried this countless ways, time to throw in the towel and ask the master... #!/bin/bash # Read in the VERSION.header file, this is a plain text file # with returns, which in some of my tests, I am loosing the returns and getting # one string wrapped only by the shell width VERSION_HEADER=`cat VERSION.header` sed 's/_VERSION_HEA...

Finding the number of files in a directory for all directories in pwd

Hello all, I am trying to list all directories and place its number of files next to it. I can find the total number of files ls -lR | grep .*.mp3 | wc -l. But how can I get an output like this: dir1 34 dir2 15 dir3 2 ... I don't mind writing to a text file or CSV to get this information if its not possible to get it on screen. ...

Bash shell losing environment variables between two lines

I have a bash script as follows: rvm use 1.8.7 rvm list The first line is a function loaded within my .bashrc file that defines some enviroment variables. When executing the second line, those variables have been set to their previous values (the set values have been lost). What am I missing here? Running on a ubuntu box. ...

Reliable method to block spammers in Rails App?

I'm receiving many failed login requests from spammers/bots that are trying to brute-force the credentials, also I'm receiving many requests to pages like /forum/index.php. I wrote a script to parse the IP's of those attackers from production.log: #!/bin/bash # Failed Logins grep "Failed " ~/app/log/production.log | egrep -o -e "[0-9]...

Shell scripting - running a regex to modify a file in place, that includes the file name.

I have many html files named 12345.html, 12346.html, etc. I need to change "style.css" to "style-12345.css" or the appropriate file name. I'm not sure what tool to use, recommendations? ...

How to create special files that talk to BASH shell

I'm trying to use someone else's [c] code that controls a linux shell over a wireless usb device. It fopen-s files "IReadFromBash" and "IWriteToBash" in the current dir to communicate. It comes with no notes but obviously expects a special file to already exist to facilitate this, and segfaults without them. Is there some obvious way to ...

determining if process has terminated successfully

Hi, I have some code to execute the unix shell command in background in python import subprocess process = subprocess.Popen('find / > tmp.txt &',shell=True) I need to capture the scenario where i come to know that process has finished successful completion . Please explain with sample code Tazim ...

matching columns from two files

Dear all, I have two files, in which the first column of them might have same values. I would like to match the first column of both files, and print the lines in FILE1 for which there was a match. FILE1: xxx1 yyy yyy yyy xxx2 yyy yyy yyy xxx3 yyy yyy yyy FILE2: xxx3 zzzz xxx4 zzzz OUTPUT: xxx3 yyy yyy yyy Any suggestions are welco...

[bash] don't know howto deal with dir/file with spacing as glob in bash scripting

Since i have some video courses in my laptop. I now want to calculate the total time of every course. So i write a simple script with bash. It succeeds for file/dir without space. But for dir/files with space, it goes wrong. I have tried to set IFS to something else, it doesn't work either. Well, there also some bugs in it. #!/bin/ba...

Variables value gets lost in subshell

This bash script concatenates the names for jar files to a classpath (variable CP), in the while loop the value is correct but is lost in the subshell as descibed in this related question http://stackoverflow.com/questions/124167/bash-variable-scope #!/bin/bash CP="AAA" func() { ls -1 | while read JAR do ...

Rejecting files with Windows line endings using Perforce triggers

Using Perforce, I'd like to be able to reject submits which contain files with Windows line endings (\r\n IIRC, maybe just \r anywhere as really we only want files with Unix line endings). Rather than dos2unix incoming files or similar, to help track down instances where users attempt to submit files with Windows line endings, I'd like ...

What are your LS_COLORS?

After installing a full version of Cygwin, I open up the MinTTY shell and I like the green on black. However, when I do an 'ls', I get a dark blue for directories. It's not very readable. I found that the LS_COLORS environment variable controls the output of ls. Here is my current default: no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=...

Linux bash - break a files into 2-word terms

Hi, I have put together this one-liner that prints all the words in a file on different lines: sed -e 's/[^a-zA-Z]/\n/g' test_input | grep -v "^$" If test_input contains "My bike is fast and clean", the one-liner's output will be: My bike is fast and clean What I would need now is a different version that prints all the 2-word terms i...

Bash's equivalent of Tcsh's ESC-p to jump to command starting with what you typed so far.

I recently made the insanely long overdue switch from tcsh to bash. The only thing I miss is tcsh's ESC-p feature: Start typing a command and then hit ESC-p (I actually found the equivalent ctrl-[p easier to type) and it jumps to the most recent command in your history that starts with what you've typed so far. Perhaps the best answer i...

What is the best way of removing reoccurring lines from a file in Bash?

Folks, I have a file that contains ldap entries and I want to remove "version: 1" lines from the second occurrence and on. I know sed can do things like this, but since I am very new, I don't know how to proceed. This is a Solaris 10 machine and the file looks like as follows: version: 1 dn: uid=tuser1,ou=people,o=example.com,o=isp cn:...

testing command line utilities

I'm looking for a way to run tests on command-line utilities written in bash, or any other language. I'd like to find a testing framework that would have statements like setup: command = 'do_awesome_thing' filename = 'testfile' args = ['--with', 'extra_win', '--file', filename] run_command command args test_output_was_...