bash

modify config file using bash script

I'm writing a bash script to modify a config file which contains a bunch of key, value pairs. How can i read the key and find the value and possibly modify it? ...

How to get the full pathname of the current shell script?

Is there a less brute-force way to do this? #!/bin/ksh THIS_SCRIPT=$(/usr/bin/readlink -f $(echo $0 | /bin/sed "s,^[^/],$PWD/&,")) echo $THIS_SCRIPT I'm stuck using ksh but would prefer a solution that works in bash too (which I think this does). ...

Can this be done by sed?

Hiall,I need to deal with a file which seems as follows: 1234 4343 5345345 53453 4343 And what I want to do is to execute follow command to the number of each line: grep $num1 ./somepath #get num1_res Then write $num1 and $num1_res to another file which will be: 1234 32 4343 234 5345345 349 53453 78 #...etc Any good solution by...

How to kill all asynchronous processes

Suppose we have a BASH script running some commands in the background. At some time we want to kill all of them, whether they have finished their job or not. Here's an example: function command_doing_nothing () { sleep 10 echo "I'm done" } for (( i = 0; i < 3; i++ )); do command_doing_nothing & done echo "Jobs:" jobs sleep 1 ...

Script doesn't work when executed with sudo

Linux bash script: function Print() { echo $1 } Print "OK" This script runs successfully, when executed directly, and gives an error running with sudo: alex@alex-linux:~/tmp$ ./sample-script OK alex@alex-linux:~/tmp$ sudo ./sample-script [sudo] password for alex: ./sample-script: 1: Syntax error: "(" unexpected Why? ...

Prevent * to be expanded in the bash script

Linux bash script: #!/bin/bash function Print() { echo $1 } var="*" Print $var Execution results: alex@alex-linux:~/tmp$ ./sample-script sample-script "*" is expanded to the list of files, which is actually script itself. How can I prevent this and see actual variable value? In general case, var can be more complicated tha...

How to parse POST data in a CGI script with BASH scripting?

Hi everybody I Have a cgi script written with bash and i have to read a POST variable sent to this file. I am not good at bash scripting so i really need this help. From a php script I send a POST variable named log_message to this cgi but i don't know how to parse the POST var from the header. Any help? ...

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

Messy bash variable

I'm writing a script to ssh in to a list of machines and compare a variable to another value.. I've run into a problem (I have a couple workarounds, but at this point I'm just wondering why this method isn't working). VAR=`ssh $i "awk -F: '/^bar/ {print \$2}' /local/foo.txt"` ($i would be a hostname. The hosts are trusted, no password...

Temporary operation in a temporary directory in shell script

I need a fresh temporary directory to do some work in a shell script. When the work is done (or if I kill the job midway), I want the script to change back to the old working directory and wipe out the temporary one. In Ruby, it might look like this: require 'tmpdir' Dir.mktmpdir 'my_build' do |temp_dir| puts "Temporary workspace is ...

": > 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 ...

Improved technique to store a filename in a variable?

Greetings, I need to store the filename of a log into a variable so my script can perform some checks on the daily log files. These logs always have a different name because they have a timestamp in the name. Currently I'm using a hodge podged method that pipes an ls command to sed, sort, cut, and tail in order to get the name out. C...

How do I extract the values from this data using bash and awk?

I grepped these, how do I extract the values? ... cavity_2mgl_wt_strip57001.out: Total cavity volume (A3) : ( 1.240E+01) cavity_2mgl_wt_strip58001.out: Total cavity volume (A3) : ( 2.408E+00) cavity_2mgl_wt_strip60001.out: Total cavity volume (A3) : ( 4.935E+00) cavity_2mgl_wt_strip61001....

Bash loop command until file contains n duplicate entries (lines)

Hello, I'm writing a script and I need to create a loop that will execute same commands until file does contain a specified number of duplicate entries. For example, with each loop I will echo random string to file results. And I want loop to stop when there are 10 lines of of the same string. I thought of something like while [ `so...

Bash Scripting: I want to open a set of .php files, and add line before html tag

Hi guys, I have a set of .php files in a folder, I want to add text just before these lines: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" > What i want is to insert just before these lines...

How to remove first comma on each line in a file

Remove first comma on each line in a file. I presume sed is needed. ...

How do I use a file grep comparison inside a bash if/else statement?

When our server comes up we need to check a file to see how the server is configured. We want to search for the following string inside our /etc/aws/hosts.conf file: MYSQL_ROLE=master Then, we want to test whether that string exists and use an if/else statement to run one of two options depending on whether the string exists or no...

How to get the first numbers with SED linux

Hello i have this line... "00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter" how can i get the first numbers until VGA with SED in Bash script? Thanks! ...

Bash: how to process variables from an input file?

I've got a bash script that reads input from a file like this: while IFS="|" read -r a b do echo "$a something $b somethingelse" done < "$FILE" The file it reads looketh like this: http://someurl1.com|label1 http://someurl2.com|label2 However, I'd like to be able to insert the names of variables into that file when it suits me,...

Display contents of a file in the parent directory

I have a command which lists Weblogic instances directories on a server.I want to display contents of a file in the parent directory of each directory listed. An additional feature would be to display the name of the file in addition to displaying the contents /usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | gr...