bash

Bash: not absolute script #! interpreter?

Is it possible to say to a script to search an interpreter not in an absolute location, but in his path? example: Having to write #!/usr/bin/php is totally retarded: what if the user have php installed somewhere else? Just writing #!php does not automagically works, of course, and I couldn't find anything about it, they just all treat...

How do I force the user to become root

I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo). Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username? ...

How to compare the size of two directories?

I want to compare the total size of two directories dir1 and dir2 on different file-systems so that if diff -r dir1 dir2 returns 0 then the total sizes will be equal. The du command returns the disk usage, and its option --apparent-size doesn't solve the problem. I now use something like find dir1 ! -type d |xargs wc -c |tail -1 to k...

Find a file and open it for editing. How to do it fast in a bash shell ?

I face many times this simple and repetitive task configuring the LAMP or some stuff in Ubuntu or Drupal: I have to edit a config file (php.ini, httpd.conf, ... whatever) so quite frequently, if I don't remember the path by heart, I run these 2 commands: locate php.ini ------- typing manually one of the paths that are shown in the list...

How do I redirect output of nested function calls in bash?

I have a bash script that has a few functions which are all called within 1 function. How can I pipe all the output from all the functions up to the main one? I'll be using tee as well to display that output to term and to a log file. func 1 func 2 func 3 func 1 func 4 func 2 func 3 call func 4 # i want to grab it here...

xargs with multiple arguments

I have a source input, input.txt a.txt b.txt c.txt I want to feed these input into a program as the following: my-program --file=a.txt --file=b.txt --file=c.txt So I try to use xargs, but with no luck. cat input.txt | xargs -i echo "my-program --file"{} It gives my-program --file=a.txt my-program --file=b.txt my-program --file=...

Ubuntu: exit sudo after a bash command?

I have Ubuntu 10.04. Certain operations in the Terminal application or operations require me to type sudo [whatever command] and my password. I have noticed the effects of sudo last for awhile; five minutes or so. How can I explicitly exit sudo and go back to being me at the command prompt? ...

Command to find all files in a directory and concat them for a parameter?

Hi, I'm looking for a command which finds all files in a directory using a specific partern, lets say "*.txt" and create a list of parameters from it in BASH. So if the dir contains: file1.txt file2.txt file3.txt nonsense.c I need the string "file1.txt file2.txt file3.txt" I knew there was a BASH/Unix command for this, but I can't re...

Regex process using cut(Linux)(sh)

Hello people, I'm having a problem with regex in using the command sh cute, the problem is that I want to show all processes that start with g and just show the command, but do not know, help me please? To do this I use the command: ps aux | grep g but this show all process who contains the letter g and i need who start with g and c...

Nmap in a bash script / only check the "80" open ports...

Hi, I'm completely new to bash scripting and I'm trying to get this working: Scanning an ip range for finding devices with the port 80 open... I think it has to look like this: #!/bin/bash echo ----------------------------------- for ip in 192.168.0.{1,.255}; do nmap -p80 192.168.0.1 if #open; then echo "{ip} has the ...

Rename multiple sequentially numbered files and change numbering format using BASH?

Hey all,     I have a bunch of sequentially named files in this format: imageXXX.jpg. So it would be like image001.jpg and onward. I just want to keep the number part of this, and get rid of the prepended 0's. So instead, that file would be named 1.jpg. How could I achieve this using BASH? Thanks!   -Trey ...

diff exclude pattern

I have a question about the diff command if I want a recursive directory diff but only for a specific file type, how to do that? I tried using the exclude option but can only use one pattern only: $ diff /destination/dir/1 /destination/dir/2 -r -x *.xml with the command I can only exclude xml file type, even though there are files in ...

How can I remove all but the last 10 lines from a file?

Is it possible to keep only the last 10 lines of a lines with a simple shell command? tail -n 10 test.log delivers the right result, but I don't know how to modify test.log itself. And tail -n 10 test.log > test.log doesn't work. ...

No result on standard output when running expect

Whenever I try to run the script it doesn't show me any result on standard output. #!/usr/bin/expect -- send [exec tail -f /var/opt/jboss/log/jbossall.log | grep -i "pattern"] Please advise the reason. ...

How can I control the Perl version used when submitting grid jobs?

I'm working with SGE (Sun Grid Engine) to submit jobs to a grid. I also use perlbrew to manage my installed Perl versions. I wrote some short sh scripts that I use to run a perl script which requires a specific Perl version (5.12.2), which look something like this: #!/bin/bash #$-S /bin/bash source /home/dave/.bash_profile /home/dave/p...

What command do you use to preview any image

I'm trying to write a command in sh using the software ImageMagick. I was trying to find a command that allows me use any image( presumably jpeg) and be able to preview the image but at a 200x200 pixels instead of the original size? ...

preview multiple thumbnails of images

I have this piece of sh code here which takes one argument at a time: #!/bin/sh clear if [ $# -eq 0 ] then echo -n "There are arguments...Please enter again with the arguments" echo exit elif [ ! -f "$*" ] then echo -n "The image file '$*' doesn't exist!" ...

bash alias with argument and autocompletion

I have a bunch of scripts in directory that exists on the path, so I can access each wherever I am. Sometime those are very simple util scripts that "vims" the file. From time to time I would like to quickly see the content of script file and see path to file the script opens (then make cat, grep ...). I would like to make an alias whi...

bash templating

i have a template, with a var LINK and a data file, links.txt, with one url per line how in bash i can substitute LINK with the content of links.txt? if i do #!/bin/bash LINKS=$(cat links.txt) sed "s/LINKS/$LINK/g" template.xml two problem: $LINKS has the content of links.txt without newline sed: 1: "s/LINKS/http://test ...": bad...

Compiled vim from source on Mac OSX and getting 'shell returned 127' error?

Hello. On my Mac OSX 10.6 machine, I started to use MacVIM for vim. Then I noticed my terminal version was different to the MacVIM version (MacVIM was 7.3 and my terminal version 7.2)... I thought they used the same vim? I need 7.3 for some of the features. So I set about wanting to update the terminal version. I basically cloned it us...