shell-scripting

Shell Script - print selected columns

Hi, I have a txt file with columns separated by tabs and based on that file, I want to create a new file that only contains information from some of the columns. This is what I have now awk '{ print $1, $5 }' filename > newfilename That works except that when column 5 contains spaces e.g 123 Street, only 123 shows up and the stree...

Read a variable in bash with a default value

I need to read a value from the terminal in a bash script. I would like to be able to provide a default value that the user can change. # Please enter your name: Ricardo^ In this script the prompt is "Please enter your name: " the default value is "Ricardo" and the cursor would be after the default value. Is there a way to do this i...

Bash command that prints a message on stderr

I want to know if there is a built-in BASH command that prints some text on stderr, just like the echo command that prints text on stdout. I don't want to use temporary io-redirection. I use a built-in command to generate an error on stderr such as ls --asdf (ls: unrecognized option '--asdf') but I want something neater. Edit ---- Actu...

Use matching value of a RegExp to name the output file.

I have this file "file.txt" which I want to split into many smaller ones. This a piece of it: 0 id:2293 7:0.78235 12:0.69205 17:0.79421 21:0.77818 .. 4 id:2293 7:0.78235 8:0.97904 12:0.69205 17:0.31709 .. 1 id:2294 7:0.78235 8:0.90994 17:0.49058 21:0.59326 .. Each line of the file has an id field which looks like "id:1"...

can anyone tell me what -a in unix means?

Can anyone tell me what -a means in unix. Please see the following code: if [ "$x" = "$x" -a "y" -eq 0 ] ; then echo $x else echo $y ...

What will be the return status in the following shell script.

i have two shell scripts: script1.sh echo "`date` : DATE" exit 0; script2.sh ./scripts1.sh if [ $? -eq 0 ]; then reboot; else echo "Failed" fi What should be the result. Ideally it should have been "reboot" , however on some execution i got the "failed" message too. Can anyone explain why this happens? ...

How to force emacs to use \n instead of \r\n

I have to use windows to write some shell scripts. I decided to use emacs, but I get a weird error when running the script: /bin/bash^M: bad interpreter: No such file or directory Correct me if I'm wrong, but that looks like the shebang ends in \r\n instead of just \n. How can I tell emacs to only write \n? I'm in Shell-script major...

Stop execution of python script when parent Bash shell script is killed

I'm working on a Bash shell script that runs several Python scripts like so: cd ${SCRIPT_PATH} python -u ${SCRIPT_NAME} ${SCRIPT_ARGS} >> $JOBLOG 2>&1 At one point, I killed the shell script (using kill PID), but the Python script continued running, even after the script terminated. I thought these would die as soon as the main script...

Checking for a dirty index or untracked files with Git

How can I check if I have any uncommitted changes in my git repository: Changes added to the index but not committed Untracked files from a script? git-status seems to always return zero with git version 1.6.4.2. ...

Test condition precedence in shell scripts

In the following, does (exp3 -a exp4) get evaluated first? How would I make it more explicit? if [exp1 -o exp2 -o exp3 -a exp4 -o exp5] then ... fi ...

Parsing result of Diff in Shell Script

I want to compare two files and see if they are the same or not in my shell script, my way is: diff_output=`diff ${dest_file} ${source_file}` if [ some_other_condition -o ${diff_output} -o some_other_condition2 ] then .... fi Basically, if they are the same ${diff_output} should contain nothing and the above test would evaluate to tr...

why is the line #!/bin/ksh is the first line in a shell script

#!/bin/ksh is the first line in a shell script always what is the importance of this line and why is it kept as the first line. ...

GET command is giving two kinds of ouput,why???

iam using GET command to get the content of a page.When i write the same command on shell prompt it gives correct result but when i use that in PHP file then sometimes its giving correct result but sometimes it gives only half of the content i.e. end-half portion only. Iam using following command in shell script :- GET http://www.abc.c...

Replace text from a MySQL dump using cat (or something else) in shell

I've got a database dump in MySQL where I want to replace ocurrencies of paths that are in the dump as plaintext. In this particular case I'd like to replace: /var/www/ for /home/www/ How could I safely do that using cat or any other shell tool? ...

process killed -- delete output file?

I have a bash script that runs on our shared web host. It does a dump of our mysql database and zips up the output file. Sometimes the mysqldump process gets killed, which leaves an incomplete sql file that still gets zipped. How do I get my script to 'notice' the killing and then delete the output file if the killing occurred? Edit: ...

Compatibility of x-www-browser

I want to open html files from a shell script. I know that Ubuntu has a command x-www-browser that will open the default browser on the system. I also found via some Googling that the command is part of the debian system. I was wondering if the command is available on non debian based distros. If it isn't is there a standard way of openi...

Bourne: if statement testing exit status

What is the difference: if IsServerStarted ; then ... and if [ IsServerStarted -eq 0 ] ; then ... Seems to me that these two statements should be equivalent? Strangely the second statement is always true. ...

How can I quickly sum all numbers in a file?

I have a file which contains several thousand numbers, each on it's own line: 34 42 11 6 2 99 ... I'm looking to write a script which will print the sum of all numbers in the file. I've got a solution, but it's not very efficient. (It takes several minutes to run.) I'm looking for a more efficient solution. Any suggestions? ...

Reading into directory, unix shell script

I am trying to analyze the files/directories inside of a directory using a shell script, for example if the file is readable, if it is a file, if it is a directory, etc. My script is set up to take a directory as input. so I would type 'file.sh directoryname'. However, when I create a for loop to analyze the files, it analyzes the files ...

SQL0401N The data types of the operands for the operation "" are not compatible. SQLSTATE=42818

!/bin/ksh # while read line do echo "$line \ n" db2 "Select mandt,kunnr,katr6, bran5 from sapr3.kna1 where mandt='030' and kunnr=$line for read only with UR">> result.txt done and I am getting SQL0401N The data types of the operands for the operation "" are not compatible. SQLSTATE=42818 I want these four field in the...