bash

How to create a CLI program with "shell prompt"?

Hi, I'm very new in programming. The following task looks very simple but I don't know how to do it. Appreciate if anyone can give me some guidance. I'm using Linux OS. I would like to create a CLI program and let the user run it in shell terminal. I plan to use Bash shell script to create this program, but C++ or Perl should be ok too...

How to fix path variable in bash on Mac OSX Snow Leopard

This might be a noob question, but I need help. I screwed up my terminal by trying to alter my path variable using the following command: $ sudo nano .profile Before I did that, if I were to type: $ echo $PATH I would get: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin When I opened .profile in nano it told me that the fil...

Problem with my Bash script

He all, i have a problem with my bash script. That's my code: #!/bin/bash java -jar my_app.jar echo "The present working directory is `pwd`" If i exec it by ./script_name it work, but if i double click on it don't work, i got this error: "Unable to access jarfile my_app.jar". Then the pwd output is different !!! My OS is MacOSX but i...

Bash script for searching and replacing tags.

I have a file have lots of string tags like </usr/share/some_pattern>, all they have in common is they all begin with a < and end with a >, how can write a bash script to remove all these tags(including the strings inside)? ...

Bash: subtracting 10 mins from a given time.

In a bash script, if I have a number that represents a time, in the form hhmmss (or hmmss), what is the best way of subtracting 10 minutes? ie, 90000 -> 85000 ...

How to define environment variable in input command of su

This command has an empty output. su user -c "ABC=abc;echo $ABC" Any idea, how can I define a variable in the input command? ...

Bash: delete based on file date stamp

I have a folder with a bunch of files. I need to delete all the files created before July 1st. How do I do that in a bash script? ...

Package bash script in "executable" for double-click execution (ideally platform independent)?

I wrote a number of bash scripts that greatly simplify the routine, but very tedious, file manipulation that my group does. Unfortunately, most in my group cannot open a terminal, let alone run scripts with complex arguments. Is there a way to nicely package a bash script into an executable (that accepts arguments) that runs nicely on ...

Only get hash value using md5sum (without filename)

I use md5sum to generate a hash value for a file. But i only need to receive the hash value, not the file name. md5=`md5sum ${my_iso_file}` echo ${md5} 3abb17b66815bc7946cefe727737d295 ./iso/somefile.iso How can i 'strip' the file name and only remain the value ? ...

Can't find out why my bash script is exiting after access denied

I have a part of my script that does this: Removes everything in directory Force syncs from perforce that directory copies files from another directory to said directory, of which there are some conflicts that the source control prevent from being overwritten (which is expectable and what I want) Before I would have just this: ... c...

Running a shell command from Ruby: capturing the output while displaying the output?

I have a problem. I want to run a ruby script from another ruby script and capture it's output information while letting it output to the screen too. runner #!/usr/bin/env ruby print "Enter your password: " password = gets.chomp puts "Here is your password: #{password}" The script file that I run: start.rb output = `runner` puts o...

Processing MySQL result in bash

Hi, I'm currently having a already a bash script with a few thousand lines which sends various queries MySQL to generate applicable output for munin. Up until now the results were simply numbers which weren't a problem, but now I'm facing a challenge to work with a more complex query in the form of: $ echo "SELECT id, name FROM type O...

Using time command in bash script

I'd like to use the time command in a bash script to calculate the elapsed time of the script and write that to a log file. I only need the real time, not the user and sys. Also need it in a decent format. e.g 00:00:00:00 (not like the standard output). I appreciate any advice. ...

Linux crontab doesnt launch a script

I have this user crontab (accessed via the command crontab -e): # m h dom mon dow command */3 * * * * sh /home/FRAPS/Desktop/cronCheck.sh The script cronCheck.sh looks like that: #!/bin/sh SERVICE='Script' if ps ax | grep -v grep | grep -i "$SERVICE" > /dev/null then echo "######## $SERVICE service running, everythin...

executing shell command in background from script

how can I execute a shell command in the background from within a bash script, if the command is in a string? For example: #!/bin/bash cmd="nohup mycommand"; other_cmd="nohup othercommand"; "$cmd &"; "$othercmd &"; this does not work -- how can I do this? ...

Bash: check if an array contains a value

In Bash, what is the simplest way to test if an array contains a certain value? EDIT: with help from the answers and the comments, after some testing, I came up with this: function contains() { local n=$# local value=${!n} for ((i=1;i < $#;i++)) { if [ "${!i}" == "${value}" ]; then echo "y" ...

How Can I Mix Math with Regex's in Awk or Sed?

Hi All, I've used regex's in sed before and used a bit of awk, but am unsure of the exact syntax I need here... I want to do something like: sed -i 's/restartfreq\([\s]\)*\([0-9]\)*/restartfreq\1$((\2/2))/2/g' \ my_file.conf where the second match is divided by 2 and then put back in the inline edit. I've read though that sed ca...

Benefits of using Ruby FileUtils instead of Bash commands?

What are the benefits of using FileUtils methods http://ruby-doc.org/core/classes/FileUtils.html than the equivalent Bash commands? ...

Bash script log file rotation

My bash script produces a log file. Now i'd like to implement some log file rotation. Let's say the first time it's called somelog.log, the next time it's renamed to somelog.log.1 and the new log file somelog.log.The third time the new log is somelog.log again, but somelog.log.1 is renamed to somelog.log.2 and the old somelog.log to some...

Can this be done faster (read file, substitute [sed], write new file)

I use this piece of code in my bash script to read a file containing several hex strings, do some substitution and then write it to a new file. It takes about 30 minutes for about 300 Mb.I'm wondering if this can be done faster ? sed 's,[0-9A-Z]\{2\},\\\\x&,g' ${in_file} | while read line; do printf "%b" ${line} >> ${out_file} printf ...