bash

How do you write a bash script function that is dynamic to the parameters passed?

I have a bash function (slightly simplified for explaining) copy_to() { cp $1 $2 $3 } This works fine: copy_to -f /demo/example1.xml new_demo/ But Let's say I want to copy all the xml files, the following code will have issues: copy_to -f /demo/*.xml new_demo/ Obviously I could just write cp -f /demo/*.xml new_demo/, but is ...

How to check if running in Cygwin, Mac or Linux?

I have a shell script that is used both on Windows/Cygwin and Mac and Linux. It needs slightly different variables for each versions. How can a shell/bash script detect whether it is running in Cygwin, on a Mac or in Linux? ...

bash quote problem

http://pastebin.com/GvZVS7a7 help please Edit Here's the code from the pastebin: d=`echo \"$1\"` echo $d #command run ./command 2 #expected output "2" ...

Batch renaming files using UNIX - should I use awk?

I have a large number of image files that i need to rename from the comand line. I believe the best way to do this is to use awk (please correct if this is incorrect). file names are like the following 1038282829128738912-1.jpg Every image file starts with '103' and I want to replace this part of the file name with '201003' - leaving...

Find all .docx files, add sufix with bash

It's been a while since i used bash:/ I wish to find all .docx files, and append them a string. This is my current code, witch has a little bug:find -name '*.docx -execdir mv {} {}$string \; Files are renamed, but string is added like this filename.docx_string and not like that filename_string.docx. Any ideas? Thank you:) ...

How to run script commands from variables?

I tried to run commands using pipes. Basic: single="ls -l" $single which works as expected Pipes: multi="ls -l | grep e" $multi ls: |: No such file or directory ls: grep: No such file or directory ls: e: No such file or directory ...no surprise bash < $multi $multi: ambiguous redirect next try bash $multi /bin/ls: /bin/ls: c...

Extract 1 file from tar.gz with bash

Is it possible to programmatically pull a single file from a decently sized .tar.gz without extracting the entire tarball to disk? Essentially I need to get inside large tar.gz files over the network and extract 1 small text file. It seems somewhat over-the-top to pull and extract the tarball to disk, then pull the file out, then delete...

Copy files that are different?

I have two directories with similar files, (one being a backup), what is a command that allows me to copy over only the files that are different to the backup? cp -rf firstDir/* secondDir is awfully inefficient, since a lot of the files are the same. ...

How do you tell if the current terminal session is in GNU screen?

I have a command I want to execute in .bashrc only when the current terminal window is managed by GNU screen. How do I tell it? Is there an environment variable for it? I used to have if [ -n "$WINDOW" ]; then command fi but, from what I can tell, $WINDOW may or may not be defined across all screen managed sessions ...

Why does recursive mode on chmod do everything but recursion?

chmod -R 775 *.cgi only changes permissions on the files in the current directory, the files in the subdirectory don't get modified at all. This is the exact same functionality as simply doing chmod 775 *.cgi. I have seen people use solutions such as with find and whatnot. Ok great, but why does -R mode even exist if it doesn't even acco...

SSH Bash script for easy connection

I have a bash script which I wrote that lists out some servers/user names. I chose a # which then connects me to said server with said user name. So far the script works fine other then the fact that when I launch ssh, the bash script hangs. It doesn't dump me into ssh. #!/bin/bash echo `clear` SERVER1="1.) Server1/username1" SERVER2="2...

Stop on first error

How can I have bash stop on the first command failure, without putting stuff like this all through my code? some_prog || exit 1 some_other_prog || exit 1 ...

Shell-Scripting (BASH): What is the best way to toggle a script "on/off" that isn't able to end itself?

Hi, I would like to have a script with an infinite loop, that kills all processes created by this script when it gets executed again. So basically you start the script via terminal: bash poll.sh and it's running infinitely, you open another terminal and again bash poll.sh and all poll.sh processes will get killed. What would be th...

Is there for the bash something like perls __DATA__?

Is there for the bash something like perls __DATA__? I mean the feature, that the code after __DATA__ will not be executed. ...

Pipe string with newline to command in bash?

I am trying to pass in a string containing a newline to a PHP script via BASH. #!/bin/bash REPOS="$1" REV="$2" message=$(svnlook log $REPOS -r $REV) changed=$(svnlook changed $REPOS -r $REV) /usr/bin/php -q /home/chad/www/mantis.localhost/scripts/checkin.php <<< "${message}\n${changed}" When I do this, I see the literal "\n" rathe...

extract strings out of plist with bash script, maybe sed?

So here is my problem. How can I extract those strings into 3 variables with a bash script? So out of this: <key>GameDir</key> <string>C:/Program Files/</string> <key>GameEXE</key> <string>C:/Program Files/any.exe</string> <key>GameFlags</key> <string>-anyflag</string> I want: GameDir=C:/Program Files/ GameEXE=C:/Program Files/any.e...

Bash: escape characters in backticks

I'm trying to escape characters within backticks in my bash command, mainly to handle spaces in filenames which cause my command to fail. The command I have so far is: grep -Li badword `grep -lr goodword *` This command should result in a list of files that do not contain the word "badword" but do contain "goodword". ...

Is it good style to call bash commands within a Python script using os.system("bash code")?

I was wondering whether or not it is considered a good style to call bash commands within a Python script using os.system(). I was also wondering whether or not it is safe to do so as well. I know how to implement some of the functionality I need in Bash and in Python, but it is much simpler and more intuitive to implement it in Bash. H...

Best/easiest way to parse configuration parameters in Sh/Bash and php

Hello, I got in every php project (around 25!), some sh scripts that help me with routine tasks such as deployment, repo syncronizing, databases exporting/export, etc. The sh scripts are the same for all the projects I manage, so there must be a configuration file to store diferent parameters that depend on the project: # example conf...

Which eclipse should I get to work with Java, C, Bash and Perl?

I'm using eclipse to work on a project with portions written in Java, C, Perl, and bash. Which version of eclipse should I get for this? I'm currently using 3.5-CDT+perl-plugin and would really like better Java handling with out loosing the C. What plugin should i add for java? How does Eclipse handle multi-language projects? when I hi...