sh

Return a regex match in a BASH script, instead of replacing it

I just want to match some text in a BASH script, i’v tried using sed, but I can’t seem to make it just output the match instead of replacing it with something. echo -E "TestT100String" | sed 's/[0-9]+/dontReplace/g' Which will output: TestTdontReplaceString Which isn’t what I want, I want it to output: 100 Ideally I would want it to...

Why reference $? explicitly

A lot of sh code looks like: $cmd if [ $? = 0 ]; then $cmd2; fi Instead of: if $cmd; then $cmd2; fi I have typically assumed that people use the former simply because they are unaware that the syntax of the latter is valid, but I'm wondering if there is another reason (although the only possibility that comes to mind is portabili...

Redirecting standard output to new file adds strange character to end of filename in cygwin

I'm having trouble with a shell script in Cygwin. The specific command that's causing the problem is this: sed -e "s/6.0.[0123456789]\{1,\}/6.0.${REV}/g" "path/to/file/config.xml" > "path/to/file/config.xml.tmp" The problem is that the file is being created with a strange character at the end, so instead of being named config.xml.tmp,...

Shell Script Variable Quoting Problem

I have an sh script that contains the line $PHP_COMMAND -r 'echo get_include_path();' I can not edit this script, but I need the eventual command line to be (equivalent to) php -d include_path='/path/with spaces/dir' -r 'echo get_include_path();' How can I achieve this? Below is a script that demonstrates the problem. #!/bin/sh...

Problems installing Java EE SDK on Linux

I installed the Java 6 JRE on my VPS just fine, but I can't get the EE SDK installation to even run. root@vps [/usr/java]# java -version java version "1.6.0_18" Java(TM) SE Runtime Environment (build 1.6.0_18-b07) Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode) However, when I try to run java_ee_sdk-6-unix.sh: ./ ../ java_e...

Will shell scripts called from python persist after the python script ends?

As part of an automated test, I have a python script that needs to call two shell scripts that start two different servers that need to interact after the calling script ends. (It's actually a jython script, but I'm not sure that matters at this point.) What can I do to ensure that the servers stay up after the python script ends? At th...

Resources to learn sh scripting 'just like a normal programming language'

Hi, what is the best resource (book would be nice) to learn sh scripting (the "standard" shell on Unix systems) just like when i would learn a "normal" programming/scripting language ? There are lots of tutorials on certain aspects of shell scripting, they mostly deal with shells in general and unix commands and so on, but i would rat...

Bash Scripting and bc

I'm trying to write a bash script and I needed to do some floating point math. Basically I want to do something like this: NUM=$(echo "scale=25;$1/10" | bc) if [ $? -ne 0 ] then echo bad fi The problem I'm running into is $? tends to hold the output from the echo program and not the bc call. Is there a way I save the output from the...

Shell loops using non-integers?

I wrote a .sh file to compile and run a few programs for a homework assignment. I have a "for" loop in the script, but it won't work unless I use only integers: #!/bin/bash for (( i=10; i<=100000; i+=100)) do ./hw3_2_2 $i done The variable $i is an input for the program hw3_2_2, and I have non-integer values I'd like to use. How c...

": > file" VS "> file"

Is there any differences between ": > file" and "> file"? $ : > file.out $ ls -l file.out -rw-rw---- 1 user user 0 Mar 18 21:08 file.out $ > file.out $ ls -l file.out -rw-rw---- 1 user user 0 Mar 18 21:08 file.out ...

How can I test if line is empty in shell script?

I have a shell script like this: cat file | while read line do # run some commands using $line done Now I need to check if the line contains any non-whitespace character ([\n\t ]), and if not, skip it. How can I do this? ...

Floating point operations in GCC

Hello! Anyone knows where can I find the floating functions for non FPU processor (SH-3) called __mulsf3, __divsf3, __addsf3, __subsf3, __ltsf2 and __floatsisf. I read that those functions are in libgcc but linking against libgcc does not work. Also I read that SH3 devs moved those functions to another lib (maybe libfloat or libgcc_os)....

How can I check if file exists using patterns ?

I have a directory with full svn backups named like this: name1.20100412.r9.bz2 name1.20100413.r10.bz2 name2.20100411.r101.bz2 name3.20100412.r102.bz2 ... I need to check if a backup file exists using name and revision number only. I tried test but it didn't work: if [ -e name1.*.r9.bz2 ]; then echo exists; fi [: too many arguments ...

popen fails with "sh: <command>: not found"

I'm developing a server application and I recently encountered this wierd error on a testing server (Debian Squeeze). Every executable I pass to popen fails with a msg: sh: sort: not found // happens to any command This happens regardless whether I point to the full path returned by "type" or keep it short . As mentioned earlier, thi...

Logging into SO with curl

I'm working on a project and I want to log into SO via curl. I use Google as my openID provider which means that I need to log into Google first via its API. Here is the code I have so far #!/usr/bin/env sh . ./params.sh #the script with $username and $password curl --silent https://www.google.com/accounts/ClientLogin \ -d Email=$user...

sh shell code - retrieving command result.

How can I write a simple shell script that will check if someone using display :0? This does not work: if [ 'who | grep " :0 "' != "" ] then echo "hi" fi ...

Why does sh/bash set command line parameter values when trying to set environment variable?

A question on basics : While tuning environment variables for a program launched from a script, I ended up with somewhat strange behaviour with sh (which seems to be actually linked to bash) : variable setting seems to mess up with command-line parameters. Could somebody explain why does this happen? A simple script: #! /bin/sh # Mes...

POSIX SH build loop variable with elements containing spaces

Here's the code I need: #!/bin/sh x1="a1 a2" x2="b1 b2" list=SOMETHING for x in "$list" do echo $x done And the output I want: a1 a2 b1 b2 The question is: what should SOMETHING be? I want $list to behave just as $@ does. Notes: I can't use $IFS and I can't eval the entire loop. ...

How to declare and use boolean variables in shell script?

Title. The way I tried declaring a boolean variable is by: variable=$false variable=$true Is the syntax correct? Also, if I wanted to update that variable would I just do the same format? Finally, is the following the proper syntax for using boolean variables as expressions: if [ $variable ] if [ !$variable ] Thanks! Any help is...

How can I have a newline in a string in sh?

This STR="Hello\nWorld" echo $STR produces as output Hello\nWorld instead of Hello World What should I do to have a newline in a string? I'm aware of echo -e, but I'm no sending the string to echo, the string will be used as an argument by another command that doesn't know how to interpret \n as a newline. ...