shell-scripting

bash script to rename all files in a directory ?

i have bunch of files that needs to be renamed. file1.txt needs to be renamed to file1_file1.txt file2.avi needs to be renamed to file2_file2.avi as you can see i need the _ folowed by the original file name. there are lot of these files. ...

Unix - how to source multiple shell scripts in a directory?

Hello, when I want to execute some shell script in Unix (and let's say that I am in the directory where the script is), I just type: ./someShellScript.sh and when I want to "source" it (e.g. run it in the current shell, NOT in a new shell), I just type the same command just with the "." (or with the "source" command equivalent) before...

Syntax error in CShell Script

Write Script to read a positive integer number then it computes the following sequence: If the number is even, halve it If it is odd multiply it by 3 and add1 You should repeat this process until the value is 1, printing out each value and how many of these operations you performed. #! bin\csh echo "please enter any integer number :) ...

Shell Script + Write to File a String

I have a string server.ip=192.168.1.200 And I need to write the above statement completely into the file. How can this be done? This is what I'm trying to do... set client_config = xmlrpc_server.properties echo 'serverurl=http://'${IP}':8000' >> %${client_config}% echo 'port=/RPC2' >> %${client_config}% It doesn't get added to the...

grep 5 seconds of input from the serial port inside a shell-script

I've got a device that I'm operating next to my PC and as it runs it's spitting log lines out it's serial port. I have this wired to my PC and I can see the log lines fine if I'm using either minicom or something like: ttylog -b 115200 -d /dev/ttyS0 I want to write 5 seconds of the device serial output to a temp file (or assign it to ...

Bash: redirect standard input dynamically in a script

I was trying to do this to decide whether to redirect stdin to a file or not: [ ...some condition here... ] && input=$fileName || input="&0" ./myScript < $input But that doesn't work because when the variable $input is "&0", bash interprets it as a filename. However, I could just do: if [ ...condition... ];then ./myScript <$file...

pass arguments between shell scripts but retain quotes

How do I pass all the arguments of one shell script into another? I have tried $*, but as I expected, that does not work if you have quoted arguments. Example: $ cat script1.sh #! /bin/sh ./script2.sh $* $ cat script2.sh #! /bin/sh echo $1 echo $2 echo $3 $ script1.sh apple "pear orange" banana apple pear orange I want it to prin...

bash function to prompt for and return input

I'm trying to write a helper function to use in a bash script to take a variable prompt string and return the value the user inputs. What I have sits and waits for a user to input the value without displaying the prompt first, which is puzzling. It also stores the echo in the return value ($foo), and doesn't keep the value read in with...

Shell script code browser

Is there a shell script code browser like ctags for c? Probably not I think, a recommended editor for shell script code browsing would also do. ...

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,...

Export a variable from PHP to shell

I'm trying to set a variable that should be accessible from outside PHP. Ideally this should be a local variable, but environment variables are also welcome. First, I've tried putenv(), but this gives no result: $ php -r "putenv('PHP_TEST=string');" ; echo $PHP_TEST $ When i call getenv() from the same script — it results in ...

*Nix ls Command in Java

Anyone aware of a method/class/library that will allow me to easily reproduce the results of the *nix ls -l command in Java? Calling ls directly is not an option due to platform independence. eg. $ls -l myprogram.exe -rwxrwxrwx 1 auser None 1261568 Nov 15 17:41 C:\myprogram.exe ...

Runing java with JAVA_OPTS env variable

Greetings, In a shell script,I have setup JAVA_OPTS environment(to enable remote debuggin and increase memory) and then execute the jar file as follows: export JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n -Xms512m -Xmx512m" java -jar analyse.jar $* But it seems theres no effect of JAVA_OPTS env var...

one line assignment in bash

i have a whole bunch of tests on variables in a bash (3.00) shell script where if the variable is not set, then it assigns a default, e.g.: if [ -z "${VARIABLE}" ]; then FOO='default' else FOO=${VARIABLE} fi I seem to recall there's some syntax to doing this in one line, something resembling a ternary operator, e.g.: FOO=${...

How to assign the output of a command to a Makefile variable

I need execute some make rules conditionally, only if the Python installed is greater than a certain version (say 2.5). I've thought that I could do something like executing python -c 'import sys; print int(sys.version_info >= (2,5))' and then using the output ('1' if ok, '0' otherwise) in a ifeq make statement. In a simple bash she...

Shell variable expansion - indirection problem while calling a utility with env

Hi all, sorry if this is a repeated question but I was trying to figure out env, ( i.e. calling a util with a new environment). Just as an example my environment variable KDEDIRS = /usr in my current environment and lets say I type: env -i KDEDIRS=/home/newkdedir env This outputs KDEDIRS=/home/newkdedir as expected. (i.e calling se...

Writing a shell wrapper script for awk

Hi there! I want to embed an awk script inside a shell script but I have trouble to do so as I don't know where to end a statement with a ; and where not. Here's my script #!/bin/sh awk=' BEGIN {FS = ",?+" } # removes all backspaces preceded by any char except _ function format() { gsub("[^_]\b", "") } function getOptions() { ...

How do I change a shell scripts character encoding?

I am using Gina Trapiani's excellent todo.sh to organize my todo-list. However being a dane, it would be nice if the script accepted special danish characters like and . I am an absolute UNIX-n00b, so it would be a great help if anybody could tell me how to fix this! :) ...

Why does WGET return 2 error messages before succeeding?

I am using a script to pull down some XML data on a authentication required URL with WGET. In doing so, my script produces the following output for each url accessed (IPs and hostnames changed to protect the guilty): > Resolving host.name.com... 127.0.0.1 > Connecting to host.name.com|127.0.0.1|:80... connected. > HTTP request sent, aw...

Test the first 3 chars in a looong string (efficiently)

If I store a long string in a variable and need to test if the string would begin with the letters abc, what would be the most efficient way to test this? Of course, you could echo the string and pipe it to grep/awk/sed or something like that but isn't there a more efficient way (which doesn't require to scan the whole string?)? Can I ...