Questions about bash only, as opposed to other Bourne/POSIX shells. For all Bourne/POSIX-style shells, use the /shell tag instead.

learn more… | top users | synonyms

2
votes
2answers
299 views

How to send text to a command and THEN read from terminal

I'd like to know if it's possible to send some to a command's stdin and then keep reading from the terminal. The problem i'm trying to solve is executing some initialization code every time i open a ...
1
vote
1answer
623 views

Sending Emails from bash script in Solaris VM

I am trying to send emails using the following bash script: /bin/mail -s "#subject" "$emailto" < "$message" It works fine on my local machine but does not work on my VM. I looked it up and read ...
10
votes
4answers
6k views

combine output from two commands in bash

Is it possible to combine output from these two commands? node ~/projects/trunk/index.js python ~/projects/trunk/run.py run Neither command exits so I'm not sure how to do this.
1
vote
2answers
1k views

bash script that uses nc for telnet reports “: command not found OK”

If I go to a bash prompt and run the following command (to flush memcached): echo "flush_all" | /usr/bin/nc 172.16.198.130 11211 it works and returns "OK". If I put that same command in a one-line ...
1
vote
2answers
766 views

Ignore all matches to negated extglob pattern in recursive ls

I would like to recursively list all files in a directory which match a given input pattern using ls. (Oh, and this is in Bash). The only way I can think of to do this using ls is to shopt -s extglob ...
0
votes
2answers
147 views

A problem with $RANDOM range in bash

why the code below, supposed to display a random from 2-3 range, actually works as if the range was 2-4? echo $((RANDOM%3+2))
6
votes
3answers
4k views

whiptail or dialog

I am going to create script which will use user input, so I decided to use whiptail, but little bit confuse that which one is portable and will work in ubuntu 10.x and higher and CentOs5.x and higher. ...
1
vote
1answer
105 views

what is diffrence between “$var” and “${var}” [duplicate]

i have seen in many script usage of "$var" and "${var}", so I just want to know diffrence between "$var" and "${var}"
2
votes
3answers
885 views

How to stop `grep` from truncating pipe output

Is there any way to have an echo at the end of a pipe simply append to the current output rather than removing it all? For example, how can I keep the output for my cat and grep and add "END OF ...
1
vote
1answer
376 views

Why does printf ignore the IFS when printing out the result of my script?

This is a follow up to a question posted on SO. I've written a script called except which should print all filenames / directories except the ones given like so. $ ls a b c d $ rm $(except b d) $ ls ...
1
vote
3answers
795 views

Shell script Variable Structure

How can I create a variable with a file name format like : FileName pattern: SnapshotIR__somenumber.csv I tried something like : TODAY=$(date +"%m%d%Y") SNAPSHOT = $(SnapshotIR$TODAY*.csv) I ...
5
votes
3answers
2k views

Create symbolic links to files using wildcards

I want to create symlinks to multiple files: ln -s dev-*.php 's/dev-(.*\.php)/$1/' Results hoped for: site.php links to dev-site.php file.php links to dev-file.php What's the most ...
3
votes
1answer
185 views

Bash 3.0 not supporting lists?

I have written a small script that add particular IP addresses taken from a config file and then puts it in a list : WAS_IP=$(grep "<was_ip>" $CONFIG| cut -d '>' -f 2 | cut -d '<' -f 1 ...
3
votes
1answer
701 views

how to start from within my shell-script xterm which starts mplayer and plays the files that are the argument of the script

What I want to do: Start my script with a list of files as arguments. From within my script I want to start xterm that starts mplayer with these files. I tried several things. I call my script like ...
6
votes
2answers
8k views

Difference between >> and >\> operators?

RHEL 6 Is there a difference between the >> and >\> operators? I read the following blurb in a RHEL training book: "You can add standard output to the end of an existing file with a ...
3
votes
1answer
684 views

Change Xfce Theme from bash script

I have a convertible notebook, and am writing a bash script to change from standard desktop to tablet mode. I am running XFCE. Currently, the script is switching reading the orientation from a ...
4
votes
2answers
5k views

Iterate over multiple parameters with spaces in bash script

I have a bash script for my RaspberryPi (running Raspbian) that is supposed to take multiple file names as parameter(s) and play one after the other (using omxplayer). The basic structure is this: ...
4
votes
1answer
828 views

Is there a way to temporarily change the terminal colour?

I use the gnome-terminal and most of my editors with a white on black theme as I find its easier on the eyes. One of my labs requires screenshots of the terminal (with the process) to be submitted ...
20
votes
2answers
20k views

What do the scripts in /etc/profile.d do?

I am reading about basic shell scripting from Linux Command Line and Shell Scripting Bible. It says that the /etc/profile file sets the environment variables at startup of the Bash shell. The ...
1
vote
1answer
98 views

Pipe each last run command to a script

I want to use realiser to know if I should have used an alias each time I run a command in my bash terminal. The README gives the following example for ZSH: function last_command() { echo `history ...
3
votes
1answer
74 views

How to put an argument vector with an argument containing spaces in a bash variable

How can I store an argument vector with an argument containing spaces in a bash variable? For example, I want 2.sh to print b c 1.sh #!/bin/bash ARGV='a "b c"' ./2.sh $ARGV 2.sh #!/bin/bash ...
5
votes
1answer
6k views

how to compare two xml files having same data in different lines?

I have two files have same data but in different lines. File 1: <Identities> <Identity> <Id>048206031415072010Comcast.USR8JR</Id> ...
0
votes
2answers
157 views

regex, only the first occurence

I have a pipelined output from free -o -m | awk '{print $4}' I want take step by step two numbers the first and the second line. So i need two regexes. I tried with ^[0-9]{1,3}$ but this match all ...
4
votes
3answers
808 views

How to attach gnu screen without knowing tty number

After starting multiple gnu screen sessions with screen -S name1 screen -S name2 I would like to selectively attach to them. If I do screen -D -R name1 it will not attach but instead print: ...
6
votes
3answers
878 views

Tar piped to split piped to scp

So I'm trying to transfer a bunch of files via SCP. Some of these are too large to be stored on the recipient (Android phone, 4GB file size limit). The sender is almost out of space, so I can't ...
2
votes
4answers
1k views

How to write a very simple wrapper that provides default parameters?

Given a program that requires some parameters, e.g. program -in file.in -out file.out, what would be the simple-most approach to write a bash script that could be called with or without any of these ...
2
votes
3answers
1k views

Query list of servers for memory/cpu allocated

Running RHEL 6.2, trying to write a bash script to SSH to a list of remote servers, and write the CPUs and Total Memory to a file, one line per host in the following format: HOSTNAME1 CPUS: 12 ...
0
votes
3answers
2k views

Move all files to a new directory excluding that directory [duplicate]

I have the following: ~@matrix:~/btp200/assignments> ls -la total 40 drwxr-xr-x 3 molszewski1 users 4096 Feb 6 13:20 . drwxr-xr-x 6 molszewski1 users 4096 Feb 6 13:14 .. -rw-r--r-- 1 ...
16
votes
3answers
7k views

Pseudo files for temporary data

I often want to feed relatively short string data (could be several lines though) to commandline programs which accept only input from files (e.g. wdiff) in a repeated fashion. Sure I can create one ...
1
vote
1answer
471 views

bash autocomplete not working, does not 'complete'

I my bashrc. I have a bash completion for the command scp implemented as follows function _scp_complete { COMPREPLY="" COMPREPLY+=( $(cat ~/.ssh_complete ) ) COMPREPLY+=( $( find . ! -name . ...
3
votes
1answer
449 views

Why do new terminals in Awesome spawn with SHLVL>1?

I have a PS1 which shows the SHLVL if it's not 1 to quickly see if I'm in a subshell. This works as expected when using GNOME, but when I spawn a new terminal in Awesome WM (Mod4+Return) it always ...
2
votes
3answers
911 views

Howto recursively create PDF thumnbails on linux command line

I am able to use ImageMagick to create a thumbnail of the first page of a PDF using: convert -thumbnail x80 95.pdf[0] thumb_95.png This works fine and generates a thumb_95.png file. I have tried ...
1
vote
2answers
1k views

How to remove element from the list?

I have written a command that output a list of hostnames, e.g. : clab148node11 clab148node12 clab148node15 clab148node16 Now, I would like to remove those nodes that do not respond to ping. For ...
0
votes
1answer
197 views

file descriptor problem with looping bash script

I've inherited a script that does this in a function: # IO redirection for logging. #touch $LOGFILE exec 6>&1 # Link file descriptor #6 with stdout. # Saves stdout. ...
8
votes
2answers
10k views

Extracting a string, according to a pattern, in a bash script

In bash, suppose that I have a string strname: strname="ph7go04325r" I would like to extract the characters between the first "3" character and the last "r" character in strname, saving the result ...
0
votes
1answer
80 views

Browse Bash files for a distribution

With my current OS, my LANG value is set by /etc/profile.d/lang.sh I am curious of the code for another distribution, Linux Mint for example. How could I browse this code online, or perhaps ...
6
votes
3answers
519 views

How to define a Bash function that can be used by different scripts

I have defined a bash function in my ~/.bashrc file. This allows me to use it in shell terminals. However, it does not seem to exist when I call it from within a script. How can I define a bash ...
6
votes
2answers
9k views

Good method to compare time?

I need to check for the current time and abort the script if it is not proper time of day I am supposed to run it. Also, if anyone else runs it, it should abort. For example: I need my script to only ...
4
votes
3answers
262 views

find greatest last-modified files of /dirA/file, /dirB/file

Suppose we have two directories, dirA, dirB, which the exact same PDF files, just different last modified. What is a bash script (no awk) that can search through each file name (assume always in both ...
3
votes
2answers
6k views

How to use tail -f with grep to show surrounding lines

I would like to see the output in a logfile greped by only one domain but also the following two lines. Example: tail -f /var/log/apache2/modsec_audit.log |grep mydomain.de this shows all lines, ...
2
votes
2answers
113 views

Select a range of logs from daily log archives

I have a folder containing daily logs, named as : system-2013-01-01.log system-2013-01-02.log system-2013-01-03.log system-2013-01-04.log system-2013-01-05.log system-2013-01-06.log ...
3
votes
2answers
5k views

If Statement with 2 Variables - Bash

I am trying to write a single IF statement which can check two variables at once. I have two variables, for example: $VARA $VARB Both with either contain a 0 or a non 0 value. I need an if ...
29
votes
7answers
1k views

What's the equivalent to && when writing a bash script?

I apologize in advance if this is a duplicate question. I did make an effort to search/check before asking here. I'm comfortable with writing one-liners like this: foocommand && ...
0
votes
2answers
996 views

Comparison of shells? [closed]

Do shells have any actual advantages or disadvantages? They can all run any binary, they all support pipes and > (output to file). Why would one choose bash over sh, or sh over ksh, etc? Why does ...
2
votes
1answer
132 views

Log from TCP Port breaking messages on characters other than newline

I'm working on a set of scripts to monitor some external services that log to a TCP port. function handleMessage { while read message do # Handle $message done } nc -d $ipadd $port | ...
9
votes
4answers
1k views

Compute average file size

I am looking for a handy one-liner for computing the average file size in a directory. What I want is: size of all files / number of files in directory
2
votes
5answers
1k views

Test if a string has a period in it with bash

I want to run a bash command on output from Drupal's drush command-line interface. drush site-alias returns a list of webroots, first showing the name of the group, and then each site in that group. ...
10
votes
5answers
2k views

Pattern matching on path names in bash

I want to act on a list of subdirectories in a directory. Consider: for x in x86-headers/*/C/populate.sh; do echo $x; done This gives x86-headers/elf/C/populate.sh x86-headers/gl/C/populate.sh ...
4
votes
2answers
395 views

Is it possible to store ssh connection in bash script instead of $(ssh user@ip command) every time?

I think that the code that I already have is not very efficient as it has to connect each time to the same machine and execute a command. Code: tmp=$(ssh -nq $USER@$IP "$COMMAND" 2>> $LOG) ...
0
votes
2answers
713 views

Bandwidth, CPU & Memory stats returned in single line from terminal

I'm trying to create a simple monitoring system that will show a graph of sales that is linked entirely to data of bandwidth, CPU and Memory stats as they relate to sales. So if I sale 1 widget per 5 ...