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

19
votes
3answers
10k 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
522 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
487 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
980 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 ...
2
votes
2answers
2k 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
205 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
13k 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 ...
7
votes
3answers
693 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
11k 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
274 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
8k 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
195 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
6k 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
1k 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
135 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
2k 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
2k 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
3k 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
466 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) ...
1
vote
2answers
822 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 ...
1
vote
3answers
796 views

Print files with file size less than 100 bytes

Basically this script is just supposed to print out the file size and the name of all files in a directory that are under 100 bytes. This script I wrote works perfectly on my Ubuntu, but wont work on ...
4
votes
1answer
252 views

Restore default completion

I use compgen when $COMP_WORD equals 1, now I want to restore default completion when otherwise, Right now I have, _rr() { case $COMP_CWORD in 1) COMPREPLY=($(compgen -W "A ...
6
votes
3answers
1k views

bash -e exits when let or expr evaluates to 0

I have a bash script that sets -e so the script will exit on any exit status != 0. I'm trying to do some basic shell arithmetic assigned to variables and sometimes the expression equals 0 which ...
1
vote
2answers
82 views

how to get a variable's definition file

I'm trying to find the original definition declaration of a variable. What linux commands or actions will output environment variable's source definition file and all files that have changed that ...
0
votes
1answer
288 views

Move video files to directory by width

I'm trying to move video files to directory by width of that video file. For example I have ./xxx/pr0n.mkv ./pokemon.mkv After running script ./xxx/1920/pr0n.mkv ./1920/pokemon.mkv If I run ...
3
votes
3answers
3k views

How to execute a bash script on multiple directories?

I have a conv.sh script that loops over MP3 files in the current directory and converts them into OGG. I have also several directories {F1, F2,..., F20} at /path/to/Files/. How should I execute ...
1
vote
3answers
972 views

Finding lists of files with specific permissions

I looked into the find command and came up with find . -maxdepth 1 -perm 521 > test.txt to output the permissions to a text file, but is it possible to do it just using, for example, simple ...
5
votes
1answer
156 views

array[@] output all messed up?

I've got this code: Unix+=("Stock List") while read line; do result=$(wget -O - -o /dev/null "http://finance.yahoo.com/d/quotes.csv?s=$line&f=sl1&e=.csv" | tr ',' ' ' | tr '"' ' ') ...
8
votes
2answers
282 views

Difference between [[ $variable ]] and [[ -n $variable ]]?

In bash, are [[ $variable ]] and [[ -n $variable ]] completely equivalent? It appears to be the case judging by the output below, but I see both forms of usage prevalent in shell scripts. $ z="abra" ...
3
votes
1answer
71 views

Using a ; or && with $@

I'm trying to do something with a cron wrapper and my cron looks something like * * * * * root /usr/bin/wrapper cd /tmp/ && ls Then wrapper looks like #!/bin/bash "$@" When I run it I ...
2
votes
2answers
184 views

Bash variables and types

I'm making a script that validate an IP address. I do this: read pool checkIp() { local ip=$1 local stat=1 if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then ...
3
votes
1answer
816 views

Why does sendmail work differently in different shells?

The following code works when I directly run it in bash shell: SUBJECT="SUBJECT-"`date`; MAIL_FROM="abc@site.com"; MAIL_TO="abc@site.com"; MAIL_CC="abc@site.com"; MAIL_FILE="/path/of/html/body.html"; ...
3
votes
4answers
4k views

How to pipe the stdout to a file if the current command is already being piped through gzip

So I have this command I need to run then disown: innobackupex-1.5.1 --user=root --password=**** --stream=tar ./ | gzip - > /data/myfile.tar.gz How do I pipe any output to a file? adding ...
1
vote
3answers
127 views

UNIX be-spoke Shell-Script / Commands

Sorry in advanced if this question would be better suited on SO. Basically, I'm getting used to using the command line interface (linux etc..) and I come across commands like: git [command] .. ...
1
vote
3answers
148 views

How to make a script for run apps for 1 second?

I have the following bash script for running some apps in a folder for 1 second each (for testing): #!/bin/bash for app in ./*.app do myrunner $app & sleep 1 killall -9 myrunner done ...
3
votes
2answers
78 views

Are there any standards/documents which tell me how to produce such nice help messages?

This is a follow up to a previous question, where Silverrocker suggested a nice way to display a help message similar to commands like ls or du. program --help Usage: program ...
10
votes
3answers
12k views

getopt, getopts or manual parsing - what to use when I want to support both short and long options?

Currently I'm writing a Bash script which has the following requirements: it should run on a wide variety of Unix/Linux platforms it should support both short and (GNU) long options I know that ...
2
votes
1answer
758 views

Conky - string formatting

I have conky the record: ${color lightblue} Down: ${downspeed wlan0} Up: ${upspeed wlan0} Conky prints link speed, but the text moves: Down: OB Up: OB Down: 60B Up: 60B Down: 148B Up: 148B ...
1
vote
1answer
335 views

Where place own application icons? How to get the current theme icons?

I'm developing a little application for Gnome, Unity and KDE desktops. I want the application icons to be fully theme aware. Now, I'm including the icons on the application directory on ...
6
votes
1answer
225 views

Correct handling of Python2 and Python3

What is the correct way running scripts that depend on python2.x but on a system where python3.x is aliased with python. The solution should take into account the following things : Python2 ...
3
votes
1answer
74 views

How can I prevent edited commands from overwriting the original? Or revert edits?

Often I'll execute a command in bash, press the up arrow to retrieve it from history, edit it, and execute the edit. However, sometimes I'll decide to discard the edit and run a different command or ...
8
votes
1answer
1k views

How can I change screen's hardstatus color based on the logged in user?

I haven't edited/created a .screenrc file in a while, but this is what I'm currently using: # Turn off the screen startup message startup_message off # Define format of bottom ...
3
votes
2answers
2k views

Prompt “-bash-3.2$” vs. “bash-3.2$” in SunOS 5.10

When I log into a server running SunOS 5.10, my prompt initially is "-bash-3.2$" Then when I run the following commands, I can see that the prompt is slightly different after typing bash (it no ...
3
votes
1answer
220 views

Issue when setting a default path in BASH

I have a BASH scripting question. I'd like the mention that I am new to BASH scripting; however, I am not new to programming. I am developing a simple BASH script to watch a given compass project. My ...
1
vote
1answer
282 views

how to make terminal utf-8

How can I make my terminal(bash) window uft-8, that it can show utf-8 characters? my operating system is : ubuntu 12.04 my desktop : gnome-shell How about native shell(outside of desktop)?
0
votes
3answers
155 views

How to do the ESCAPE + “_” shortcut in bash?

How can I set up my bash to replace ESCAPE+_ with the last word from the previous command?
4
votes
2answers
10k views

Start X after automatic login

I have a working automatic login to a virtual console after boot. Now I want the X server to start automatically, instead of typing startx. This worked before, but after switching to systemd, X would ...
1
vote
1answer
207 views

Passing arguments to KOrganizer event reminder bash/yad scripts

I am refining a bash/yad script which runs when an event reminder is triggered in KOrganizer. (yad is a drop-in replacement for zenity. It is being actively developed and has many more features and ...