bash

What are the ways to do numeric testing in bash?

Suppose I have variable "x" in bash. How can I test if it's some number? I tried if x=5; then echo "it is 5"; fi but that doesn't work, then I tried if x==5; then echo "it is 5"; fi but that also doesn't work, then I tried if [x==5]; then echo "it is 5"; fi but that also doesn't work, then I tried if [[x==5]]; then echo "it is 5"; fi...

Defining variables for use in both Makefiles and Shell scripts

I'd like to distribute a set of build variables (which someone should append to their LDFLAGS), in a form that can both be included in a Makefile and in a shell script. What I have now is a file buildflags.conf: LDFLAGS_EXTRA="-static -foo -bar" I want my users to be able to, when they're using makefiles: include buildflags.conf LDF...

for loop / if condition in shell script

I've never done shell script before and now I'm running into a simple problem... I have a for loop which executes every time the run.sh script. To see how far the script has already run I want to print e.g. every 5000 the actual index. $counter = 0 for (( i = 0 ; i <= 5000; i++ ))do if ($i = $counter); then echo "$c...

Zip latest committed changes only

Git has the very handy archive command which allows me to make a copy of a particular commit in a .zip archive like so: git archive -o ../latest.zip some-commit This will contain the entire working tree for that commit. Usually I just need the changed files since a previous release. Currently I use this to get those files into a zip: ...

How to repeat a few characters a few times in bash?

In a bash script, I have to include the same file several times in a row as an argument. Like this: convert image.png image.png image.png [...] many_images.png where image.png should be repeated a few times. Is there a bash shorthand for repeating a pattern? ...

How stop to going to new line in Shell script

For Ex : echo "adsa " >> a.txt echo "asdad " >> a.txt in my file adsa asdad But i am looking for adsa asdad ...

alias in a script

In linux, if I put a command in a script that contains an alias, I see that it doesn't get expanded. How can I fix that? I am using bash. ...

specifying file descriptor number?

My understanding was that one could not control the file descriptor (integer) assigned by the OS when opening a new file using open(). How then is it possible in a bash shell to assign a specific file descriptor using a command like exec 5>&1 (I suppose I could find out by reading the bash sources...) ...

Using Bash to automate creation of test outputs.

I have a java program that I am trying to generate 3 outputs for, and then rename them depending on what the input file was originally called. The problem is that only the input file is being moved. I think this might be an issue regarding relative file commands. Here is my script. (Also, I'm open to suggestions on making this script b...

What's the difference between [ and [[ in bash?

I looked at bash man page and the [[ says it uses Conditional Expressions. Then I looked at Conditional Expressions section and it lists the same operators as test (and [). So I wonder, what is the difference between [ and [[ in bash? Thanks, Boda Cydo. ...

PHP exec $PATH variable missing elements

When I echo $PATH on my command line, it returns /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/MAMP/Library/bin:/usr/local/git/bin:/usr/X11/bin When I execute this php code exec('echo $PATH; whoami; less /etc/paths; 2>&1') I get string 'echo $PATH; whoami; less /etc/paths; 2>&1' (length=56) array 0 =>...

Best way to make a shell script daemon?

I'm wondering if there is a better way to make a daemon that waits for something using only sh than: #! /bin/sh trap processUserSig SIGUSR1 processUserSig() { echo "doing stuff" } while true; do sleep 1000 done In particular, I'm wondering if there's any way to get rid of the loop and still have the thing listen for the signals. ...

bash/sh if statement syntax

In various guides and scripts I come across people tend to use different syntax of if statements. What's the difference and what are best practices? I believe all the following statements, and many more variants, will return true: bar="foo" if [ "foo" = "foo" ] if [[ "foo" == $bar ]] if [ "foo" = "$bar" ] if [[ "foo" = "$bar" ]] if [[ "...

Globbing/pathname expansion with colon as separator

How can I convert a string containing glob characters such as /var/lib/gems/*/bin into a colon-separated string of filenames (i.e. PATH compatible) matching the pattern? i.e. echo /var/lib/gems/*/bin will return /var/lib/gems/1.8/bin /var/lib/gems/1.9.1/bin I want /var/lib/gems/1.8/bin:/var/lib/gems/1.9.1/bin instead. The obv...

simple command execution inside while loop - bourne shell scripting

I have a file called inp.txt which lists 3 directory names #!/bin/sh while read dirname do echo $dirname "ls -l" $dirname done < inp.txt When I run the above, I get this error: line 5: ls -l: command not found If I do just "ls" instead of "ls -l", it works fine. What am I missing here? ...

problem with bash echo function

how do i print using echo in bash so the row wont "jump" abit to the right cause of the length of the Variable can u please help me with a command that do so ...

Bash script to safely create symlinks?

I'm trying to store all my profile configuration files (~/.xxx) in git. I'm pretty horrible at bash scripting but I imagine this will be pretty straight forward for you scripting gurus. Basically, I'd like a script that will create symbolic links in my home directory to files in my repo. Twist is, I'd like it warn and prompt for overwri...

BASH subshells and sudo?

Hello -- I'm writing a bash script that needs to sudo multiple commands. I can do this: ( whoami ; whoami ) but I can't do this: sudo ( whoami ; whoami ) How do I solve this? ...

Calling a Java executable in bash

I would like to call the Java class file from compiling the following code: import java.io.*; public class hex_to_dec { private BufferedReader bufferedReader; private BufferedWriter bufferedWriter; public hex_to_dec (String stringPath, String stringPath_dec) { try { bufferedReader = new Buf...

bash - how to pipe result from the which command to cd

Hi all: I know this must be a stupid question, but how could I pipe the result from a which command to cd? This is what I am trying to do: which oracle | cd cd < which oracle But none of them works :( Is there a way to achieve this (rather than copy/paste of course)? Thanks a lot in advance! Edit : on second thought, this command...