shell-scripting

Use lines in a file as filenames for grep?

I have a file which contains filenames (and the full path to them) and I want to search for a word within all of them. some pseudo-code to explain: grep keyword or cat files.txt > grep keyword cat files txt | grep keyword the problem is that I can only get grep to search the filenames, not the contents of the actual files Thanks for...

Validating parameters to a bash script

I'm a total newbie to doing any bash scripting, but I came up with a basic one to help automate the process of removing a number of folders as they become unneeded. #!/bin/bash rm -rf ~/myfolder1/$1/anotherfolder rm -rf ~/myfolder2/$1/yetanotherfolder rm -rf ~/myfolder3/$1/thisisafolder This is evoked like so: ./myscript.sh <{id-numb...

formatting shell output into structured data?

Are there any libraries (perl, python, ruby, etc) or other means of formatting the output of shell commands to a structured data format like JSON or XML to be processed by another application? Use case: Bunch of CentOS servers on a network. I'd like to programatically login to them via SSH, run commands to obtain system stats and eventu...

Viewing unified diff with meld/vimdiff other tools

Hi, I've got a unified diff file (say my_diff.diff), containing comparison of several files from a project. What would be the best way to review all the code changes in a nice visual format, for example, using meld or vimdiff? I have figured out the way how to do it for a single diff file (ie. containing comparison of just two files)...

What is an easy way to do a sorted diff between two files?

I have two files in which some of the lines have changed order. I would like to be able to compare these. One website suggested something that looks like this: diff <(sort text2) <(sort text1) But this yields the error: Missing name for redirect. I am using tcsh. Is the command above for a different shell? Is there a better way? ...

using the passwd command from within a shell script

I'm writing a shell script to automatically add a new user and update their password. I don't know how to get passwd to read from the shell script instead of interactively prompting me for the new password. My code is below. adduser $1 passwd $1 $2 $2 ...

How can I have a PHP script run a shell script as root?

Running Fedora 9/10, Apache 2, PHP 5... Can I run a shell script as root, from a PHP script using exec()? Do I just give Apache root priveleges, and then add "sudo" in front of them command? Specifically, I'm trying to start and stop a background script. Currently I have a shell script that just runs the app, start.sh: #!/bin/bash ...

Env Variables in Python (v3.0) on Windows

Hello all. I'm using Python 3.0. How to expand an environment variable given the %var_name% syntax? Any help is much appreciated! Thanks! ...

Linux permission denied after chmod a=rwx

Hi everyone, So I have a little Linux problem, geez that will teach me to spend so many years on Windows. Anyway I did a little java app, wrapped nicely with the Java Service Wrapper script, but when I run that script: sh ./wrapper.sh console I get permission denied right away. The permission denied message is like that: eval: 1: /h...

Variable value inconsistency on BASH and CSH

May God never give you the bane of working on Solaris. Now, I am trying to run this simple shell script: #!/bin/sh input="a b c" data="123" while read eachline do data="$data$eachline" done << EOF $(echo "$input") EOF echo "$data" exit 0 On RHEL(BASH) I receive output as expected i.e "123abc", however, on Solaris I receiv...

How to run a .sh-script in an Unix console/Mac terminal?

I know it, forgets it and relearn it again. Time to write it down. ...

Svn Post Commit Hook to copy committed files to a working copy for another repository?

The aim is to maintain 2 separate repositories that are not strictly mirrored. Both repositories are be subject to update by different developers so svnsynch is out of the question. Hooking the script might not be the right solution either as I would much prefer to run the script at will rather than with every commit. Perhaps a script...

How to reverse sed output?

I'm reading from a line that's about 500 characters. How can I get sed to, instead of replacing that string with something, replace the rest of the line with something? In short, I want to remove all the text around a specified string. Deleting columns with awk won't work, because there is an indeterminate amount of characters before and...

tcsh: How to change the file extension for multiple files?

Is there a one line command in tcsh to change the extension of a set of files? In the various DOS shells, I used to use the following: ren *.abc *.def This would rename all files ending in .abc to end instead with .def. In sed terms this would perform something like the following: sed -e 's/\(.\)*\.abc$/\1.def/' on the file names...

Iterating through a list of commands in a file in Shell Programming

I have a file containing a list of unix commands like "ls -la /etc/passwd". I want to iterate through this list of commands, and take appropriate action. Currently, my code is something like: #!/bin/bash clear for cmds in `cat /tmp/cmd` do if [ $cmds ] ; then echo $cmds; fi done But, what I get is an...

How can I use lame to encode wav files within a shell script?

I'm trying to set artist information via variables with spaces in them. Lame craps out. Maybe I'm being retarded with bash? #!/bin/bash year=2008; artist="New Kids On The Block"; album="The Block"; bitrate=320; lame="lame -b $bitrate --ta \"$artist\" --tl \"$album\" --ty $year" function first_half { for (( i=1;i<10;i++ )); do ...

Losing newline after assigning grep result to a shell variable

#!/usr/local/bin/bash out=`grep apache README` echo $out; Usually grep shows each match on a separate line when run on the command line. However, in the above scripts, the newline separating each match disappears. Does anyone know how the newline can be preserved? ...

Is there a way to indicate the last n parameters in a batch file?

In the following example, I want to call a child batch file from a parent batch file and pass all of the remaining parameters to the child. C:\> parent.cmd child1 foo bar C:\> parent.cmd child2 baz zoop C:\> parent.cmd child3 a b c d e f g h i j k l m n o p q r s t u v w x y z Inside parent.cmd, I need to strip %1 off the list of para...

Find out if a command exists on POSIX system

I want to be able to tell if a command exists on any POSIX system from a shell script. On Linux, I can do the following: if which <command>; then ...snip... fi However, Solaris and MacOS which do not give an exit failure code when the command does not exist, they just print an error message to STDOUT. Also, I recently discovered ...

Shell script to emulate warnings-as-errors?

Some compilers let you set warnings as errors, so that you'll never leave any compiler warnings behind, because if you do, the code won't build. This is a Good Thing. Unfortunately, some compilers don't have a flag for warnings-as-errors. I need to write a shell script or wrapper that provides the feature. Presumably it parses the com...