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

1
vote
2answers
38 views

Remove unneeded file lock in script

I have some script using flock executable. It works well. Problem is when this script calls another script, and it creates background process. In this situation background process inherits file locked ...
2
votes
2answers
247 views

condition && command + exit

While writing a simple shell tool, I found a piece where I don't know how to get it to work. [ "$#" -ne 3 ] || echo "wrong number of arguments" && exit The above works as intended because ...
4
votes
4answers
443 views

Place job in background without freeze

Say you start the job like this foo and then desire that you want to place it in background. Typing Ctrl+Z and then bg does the trick but will freeze foo between Ctrl+Z and bg. How can I place foo ...
2
votes
0answers
26 views

Tmux log output [duplicate]

Im looking for a solution to an issue regarding tmux. One of my scripts ns2server stopped working recently when running start (using tmux) but not in debug mode (not using tmux). I found that using ...
2
votes
1answer
56 views

How does bash actually change stdin/stdout/stderr when using redirection/piping

Unfortunately I've had no luck figuring this out, as everything I find is just on the syntax of redirection, or shallow information about how redirection works. What I want to know is how bash ...
3
votes
4answers
257 views

Make many subdirectories at once

Suppose I have a directory /, and it contains many directories /mydir, /hisdir, /herdir, and each of those need to have a similar structure. For each directory in /, there needs to be a directory doc ...
0
votes
3answers
72 views

Whats wrong with this code? [closed]

#!bin/bash if [ ! -e $1 ]; then echo "error... no such file" exit else END=$(wc -l $1 | cut -d" " -f1) for (( i=1;i<=END;i++)); do echo -e " $i \n" ...
2
votes
2answers
112 views

Bash copy all files that don't match the given extensions

When I import pictures from my camera in Shotwell, it also imports the video clips. This is somewhat annoying, as I would like to store my videos in another folder. I've tried to write a bash command ...
4
votes
1answer
57 views

Is it possible to use multiple here-docs in bash?

Can one use multiple here-docs to provide input to a command in bash? $ cat <<<foo <<<bar bar $ cat <<EOF1 <<EOF2 > foo > EOF1 > bar > EOF2 bar Obviously, ...
3
votes
2answers
87 views

bash - complex prompt

I want to have really complex prompt. It should contains login, computer name, date, time, working directory, result of last command and new line symbol. I want to color each item as well. So I ...
2
votes
3answers
51 views

Shifting command output to the right

Say I'm writing a .bashrc file to give me some useful information in my login terminals and I'm telling it to run the cal command (a nice one). How would I go about shifting the calendar produced to ...
6
votes
3answers
158 views

Attempted assignment to non-variable?

We can use arithmetic operations inside shell script function: function mess { if (( "$1" > 0 )) ; then total=$1 else total=100 fi tail -$total /var/adm/messages | more } I ...
15
votes
2answers
391 views

Is dash or some other shell “faster” than bash?

I always thought that the only benefit of using dash instead of bash was that dash was smaller, and therefore many instances of dash would start faster at boot time. But I have done some research, ...
1
vote
2answers
40 views

Call background process back to stdout

Is it possible to "call" a background process output to be shown in the terminal? Example: cron starts apt-get to update the system and I want to see the output of apt-get
0
votes
1answer
75 views

Launching a program and executing a command in bash script [closed]

How can I execute a command inside ROOT that I launch within the bash script? My aim is to have ROOT execute its GUI file browser and have it open if the user does not interfere with keyboard ...
5
votes
1answer
44 views

Getting the process-id out of command launched with “su -c”

I am running a java program inside a shell and writing the process-id to a text file. So when I do this: nohup java app.Main > /dev/null 2>&1 & echo $! > /var/run/app.pid It works. ...
6
votes
3answers
127 views

Succinct way to respond to user input?

Of course you can do this sort of thing, read var; if [[ $var = 'y' ]]; then echo "Yes"; fi But is there any way to skip the first step and do something a little more like this, (ideally without ...
3
votes
1answer
42 views

scp to an alias

To transfer a folder from my local host to a remote host, I normally do: $ scp -r myFolder user@host:destFolder/ Trying to make this easier, I aliased the remote host destination: $ alias ...
2
votes
3answers
44 views

bash: regular expressions in if expression

In my script on my ubuntu machine I declare a string like this: DEBUG_PACKAGE_LIST=$(apt-cache search dbg | awk '{ print $1 }' | grep -e "-dbg") For you to help you understand my problem here is ...
3
votes
2answers
133 views

Command substitution declaration syntax in bash - which of these two is better practice? [duplicate]

This is part of a bash find loop, and I wondered which is more correct syntax and why? filename="$(echo "$i" | cut -c5-)"; filename=`echo "$i" | cut -c5-`; Both function for the purpose of getting ...
3
votes
1answer
43 views

How do I parse a string in bash into variables using a tab as the delimiter and preserve spaces?

I am in the process of writing a bash script to display various statistics, some of which come from a MySQL database. The pertinant code I'm having difficulty with is: read min max rows <<< ...
1
vote
4answers
60 views

Delete all but largest file in multiple directories

I have multiple subdirectories in /home/usr/ProjZ/programs/ that each contain multiple (or one) .js files. What I need to do is delete all but the largest .js file, if the subdirectory contains ...
8
votes
3answers
334 views

What is the difference of -a and -e in bash's conditional expressions?

From man bash: CONDITIONAL EXPRESSIONS [...] -a file True if file exists. [...] -e file True if file exists. So what is the difference between [ -a $FILE ...
2
votes
0answers
22 views

Executable exists for everything but bash & file manager [duplicate]

I am running Debian. I was trying to use truecrypt the other day. I downloaded and extracted the zip, and ran the installer. I just extracted the file, and did not install it. I then tried executing ...
2
votes
4answers
72 views

Command line 'buffer' [duplicate]

I wanted to tail the last 100 lines of a file to the same file, but the command tail -n 100 file > file doesn't work, I assume because the stdout gets written to the file 'live', before everything ...
3
votes
1answer
96 views

two if conditions in for loop bash scripting

I've trying to make two if conditions in for loop. Is it possible this? Now doesn't return anything from second if only two OK from first if. #!/bin/bash servers=("212.39.82.157" "212.39.82.157" ...
2
votes
2answers
33 views

Variable assignment outside of case statement

In many languages it is possible to assign the result of a case/switch statement to a variable, rather than repeating the variable assignment many times within the case statement. Is it possible to ...
10
votes
3answers
1k views

Explain this bash script: echo “${1##*.}” [duplicate]

Consider the following bash script: #!/bin/bash echo "${1##*.}" This script prints the extension of a file if the file name is supplied as the first command line argument, something like ...
2
votes
2answers
41 views

Read Multiple Entries from an Input File

I wrote a program in Fortran that asks me for latitude and longitude and the name of the output file every time I run it. It works great. What I have been meaning to do is run this program for the ...
0
votes
2answers
35 views

Point program without garbaging up alias (~/.bashrc)

What is the correct way to point to a program/script without passing the command through aliases in the bashrc? For example: I create a custom script MyScript.sh. I wish to create a command that can ...
5
votes
3answers
279 views

Is it safe to use a bash script to create a daemon?

I would like to create an entirely automated box (based on something like a Raspberry-Pi or OLinuXino card). My box would run a Debian based system with a read-only filesystem and a ramdisk (to avoid ...
0
votes
1answer
52 views

Compare two strings in a shell script [duplicate]

My script needs two arguments. I want to hide the error message if someone calls the script with script.sh --help so I tired this: if [ $# -ne 2 ] ; then if [ "$1" -ne "--help" ]; then echo ...
0
votes
1answer
45 views

How to evaluate a variable length in shell script?

I want to write a shell script, in which it will call different command according to the variable length. But I didn't figure it out yet. My unwork script is here: for i in n5 n25 if ${#i} == 2; ...
1
vote
3answers
149 views

What is $() in a command?

What is $() in Linux Shell Commands? For example: chmod 777 $(pwd)
2
votes
1answer
29 views

Change the PS1 prompt in a subshell sh called from parent bash

When I am running an interactive bash session, I sometimes experiment with sh. I call sh directly from this bash session. My question is, how can I change the PS1 prompt of this subshell? I would ...
1
vote
2answers
99 views

replace lines in a text with matching text

Original file 123|abc|heloo good morning friends|1|123|abc|123|abc 123|abc|heloo good morning everyone|1|123|abc|123|abc Replaced file 123|abc|heloo good morning freinds|1|123|abc|123|abc ...
0
votes
1answer
61 views

Answer password prompt programmatically via shell script

I would like to programmatically answer a password prompt using only shell script when prompted. Is it possible to automate this task in this way? I want to avoid other tools entirely - so no expect ...
0
votes
0answers
24 views

X11 Path Issues and Confusion for Installed Packages

I am using GMT (generic mapping tools) and SAC (Seismic Analysis Code), and each use X11 (or XQuartz on 10.9.4 Mavericks) to display graphics. When I want to display postscript using ghostview, I get ...
0
votes
0answers
27 views

Join 2 files on 2 columns returning 0s

I have 2 files, that are sorted, with 3 columns each. I need to merge them based on the first 2 columsn and return 4 columns, being the 2 that are matching and then the 3rd column from each file. I ...
3
votes
2answers
70 views

Move every file that is not a directory

I have a directory that I am trying to clean out that contains both files and subdirectories. What I am trying to do is simple: move all the files into another directory, but leave all the ...
1
vote
2answers
57 views

Using if statement with or to validate arguments for shell script

I'm working on a bash script to restart multiple apache instances in different environments. It's working fine except I'd like to add some logic to ensure only correct arguments are entered. I've ...
1
vote
1answer
42 views

How can I use global variable arrays in aliases or functions

I am attempting to simplify repetitive work procedures. In doing so, I am trying to write a .bashrc script which will set global path variables that aliases can refer to. Disclaimer: I'm new to linux ...
-4
votes
3answers
80 views

Birthday reminder

write a bash shell script to get the alerts of friend's birthday Suppose Birthday Date Friend's name 08-02-2014 : Prashant 08-15-2014 : prabhat 09 -16 -2014 ...
3
votes
1answer
114 views

Preserve formatting when command output is sent to a variable? [duplicate]

I have a bash script. If I run this command to: grep for certain patterns, transform the output, sort the output dedupe the output then I get one grep find per line in terminal LC_ALL=C grep ...
0
votes
2answers
77 views

how to remove all double quotes from csv except specific field

I want to remove all double quotes from my csv but not the fourth field (because the four fields represent PATH of file) Please advice how to implement this by sed or awk or perl one liner , etc ...
3
votes
1answer
27 views

awk + gsub + how to work with all kind of characters

The following awk command target is remove spaces and unnecessary quotes from CSV file and only from single words between separators awk 'gsub(/("[ ]+|[ ...
2
votes
4answers
106 views

Change to the last modified subdirectory of the current directory

I have an application which generates a couple of files in different directories at a regular interval and I need to check the latest files generated, i.e. the file which is generated in the most ...
0
votes
1answer
66 views

Writing a Bash script to run and end at specific times

I'd like to create a script that will do the following. Start at a given time during the day and end at another given time. So for example, I have a program I'd like to test, so my script would be ...
3
votes
2answers
73 views

split a file, pass each piece as a param to a script, run each script in parallel

I have a words.txt with 10000 words (one to a line). I have 5,000 documents. I want to see which documents contain which of those words (with a regex pattern around the word). I have a script.sh that ...
1
vote
1answer
43 views

When using expect: SSHFS Transport endpoint is not connected

When I run my SSHFS connection script from bash, everything works perfectly. However, when I run the same script using the expect utility, I get the following error when I try to access the folder ...