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
1answer
123 views

Chain commands with Control + C interrupt

I am trying to execute a program which loops indefinitely, and so must be forcibly interrupted. After executing the program, I want to delete it. I can do it in several steps: ./MyProg Control + C ...
0
votes
1answer
331 views

expr command used to work and stopped

I am on SunOS machine trying to get the date string of the past minute, yesterday I used this command I found on the internet expr `date +%Y%m%d%k%M` - 1 which is worked just fine and I saw the ...
1
vote
3answers
70 views

Creating a variable to a directory that persists until deletion

I'm currently using bash on Ubuntu, and I work a lot with a directory saved at /home/me/path/to/project_dir. It is a nuisance to change to the directory every time I open bash. So I'd like to save the ...
1
vote
2answers
219 views

Turning standard output hyphen into a single command

I am working with scripts that output xml and I am using xmllint to format the output. Xmllint requires a source argument, so I use the hyphen to pipe standard output to it: $> script.php ...
0
votes
3answers
1k views

Safe Directory Delete

I alias rm to rm -i so that when I mistype file* as file * I get prompted before accidentally deleting files I didn't intend to delete. Is there an equivalent idiom for directories? In particular, ...
3
votes
2answers
530 views

Limit rows max and min length

I have this input: XX3083136|bla-bla texthere texttt|asfdasfd@safdsadf.com|SADFdsafsafd|ASDfasfdsafd|DSFSAFD|dsafasfd|sadfsad| XX3083372|bla-bla-bla this is a text ...
0
votes
3answers
54 views

How to sed lines that contain only given form?

This is the input: asdfZE3033141xycf asdfINSFRHxycf and I need this output: asdfZE3033141 asdfINSFRHxycf So I used a: echo -e 'asdfZE3033141xycf\nasdfINSFRHxycf' | sed '/asdfZE3033141xycf/ ...
7
votes
3answers
2k views

cd to a directory and execute many commands

I have following script. #!/bin/bash mount /dev/sda6 /mnt/gentoo set +e cd /mnt/gentoo && mount -t proc none /mnt/gentoo/proc \ && mount --rbind /dev /mnt/gentoo/dev \ && ...
3
votes
1answer
2k views

Logger command - how to store logs in given file?

Please explain what below command does. logger_cmd="logger -p local0.info ${program-name}[$$]:" $logger_cmd "There is some files missing in ${dest-dir}" How can I write any message in log file? I ...
1
vote
2answers
87 views

Configure bash to execute command with last argument when no argument was provided

I would like to configure bash so that when I execute command (preferably from a list of commands, not any command) without an argument, it takes the argument of previous command. So, for example I ...
4
votes
3answers
5k views

Copy stdout and stderr to a log file and leave them on the console within the script itself

Using bash, how do I copy stderr and stdout to a log file and also leave them displayed on the console? I would like to do this within the script itself using an exec. I tried with exec ...
4
votes
1answer
449 views

why do background jobs hang depending on the size of the output?

I had some stress-testing scripts that were running in parallel and they would hang after finishing and would wait for a RETURN keystroke to exit. After investigating I discovered that it is not ...
2
votes
1answer
383 views

Method to perform DNS lookup step very quickly during wget

When I download any document from internet using wget. It shows maximum amount of time consumed in getaddrinfo(). I know getaddrinfo() used for DNS lookup. Tell me any method, so that this time is ...
1
vote
2answers
447 views

Why rm command unrecognized w flag in AIX?

I have a bash script that retain the last 5 copies in AIX like this: rm `ls -lt /path/the_file_* | tail -n +6` But I get this error: rm: Not a recognized flag: w UPDATE The ls command return ...
5
votes
2answers
129 views

Is adding ./bin a bad idea

I can't think of any reason why adding ./bin to my PATH environment would be a very bad idea. I usually create bin folders in my projects that I am working in and I hate doing bin/command, it would ...
2
votes
1answer
363 views

Why does command substitution fails in ssh remote call?

I have this script: exportFile(){ file=$1 skippedBytes=$2 exportCommandString=$3 #This works: #dd if=$file bs=1 skip=$skippedBytes | ssh sshConnection 'cat - >> ...
3
votes
1answer
137 views

Bind to execute command with last argument of previous command

I want to create a bind that executes g++ !$ in the same way that the shell would interpret it if I just typed it. I tried: bind '"\ee": "g++ !$"', but it doesn't execute command (just "pastes" it) ...
9
votes
2answers
2k views

How to implement package install suggestion on Debian?

I have noticed that while on Ubuntu, if I type the following: mc and it isnt installed, I get the message below: The program 'mc' is currently not installed. You can install it by typing: sudo ...
1
vote
2answers
654 views

How do I set an environment variable for sudo in MacOS?

I have installed ServiceMix on my machine, and am trying to start it. The startup script is called start.sh. When I did ./start.sh, it gave me a Permission Denied message. So I used sudo ./start.sh, ...
6
votes
2answers
3k views

How to use a variable instead of a file to output this wc command

I want to read a file from a given offset until the end of that file. I need to retrieve the number of bytes that were read during the process and also to redirect the output of the file elsewhere. ...
4
votes
4answers
2k views

`openssl`: Subject Alternative Name

I wrote this bit of code to get the Common Name of the subject field in the SSL certificate for a given domain: $ echo -e "GET / HTTP/1.1\nEOT" | openssl s_client -connect google.com:443 2>&1 ...
4
votes
2answers
7k views

find command with regex {1,2}

I have been trying to create a find command string that will find all files that end with a number 1-99 but exclude all others. e.g. I want to find myfile1 myfile99 but not myfile456 and not ...
2
votes
4answers
600 views

bash while loop that breaks at a given file size

So, I bought this book called Primes and Programming, and it's pretty tough going. Today I wrote this (simple) program from chapter 1: #!/usr/bin/env python import math def find_gcd(a,b): while ...
5
votes
2answers
262 views

[ vs [[ : which one to use in bash scripts? [duplicate]

The zsh man page, in its section on test (aka [), explicitly advises against using it at all, and urges readers to use [[ whenever possible. The relevant section states: The command attempts to ...
6
votes
4answers
177 views

Linux: Can I build a shortcut for a complex cd command without resorting to a shell script?

On a server I work on, I always have to work in a directory, let's call it /foo/bar. In that directory is a list of site directories. Inside each of those directories is a directory for each ...
16
votes
3answers
8k views

How can one run multiple programs in the background with single command?

How can one run multiple programs in the background with single command? I have tried the commands below, but they do not work. nohup ./script1.sh & && nohup ./script2.sh & -bash: ...
13
votes
1answer
246 views

Bash No Longer Changes Directory

My bash shell will no longer change directory with cd. I noticed it earlier when working and found that any new shells I opened (terminal or xterm etc) would be stuck in the home directory and could ...
2
votes
1answer
254 views

Difference between two types of shell arithmetic

What is the difference between the following two shell arithmetic commands: echo $[ $var1 - 1 ] echo $(( $var1 - 1 )) Assuming var1 = 5 for example.
3
votes
2answers
629 views

tshark dump only when string is matched

I'm working on a web capture script where I only want to dump the traffic if the HTML body or URL contains a string defined in a .txt file. Here is my tshark command which dumps src ip dest url (HTTP ...
2
votes
1answer
3k views

Minimal command to make a copy of a file [duplicate]

I often find myself copy-pasting long path in order to create a copy of a file cp /path/to/file/file1 /path/to/file/file1.bkp Is there an alternative utility that will NOT require me to type the ...
2
votes
1answer
281 views

Variable definition changes in Bash

In Bash 3.00.16, the following prints defined, and in Bash 4.2.24 it prints undefined: my_test() { local foo if [ -n "${foo+defined}" ] then echo defined else echo ...
4
votes
3answers
7k views

Bash script that automatically kills processes when CPU/memory usage gets too high

I have created a script that kills processes if CPU and/or memory usage hits 80%. It creates a list of killed processes when this happens. What can I do to improve it? while [ 1 ]; do echo echo ...
4
votes
2answers
295 views

Bash Command (History) Number In PS1

I am a noob in bash to be honest. I have Googled around and cannot find an answer to this: history lists previous commands and their number e.g. $ history 286 clear I want to be able to see the ...
2
votes
1answer
5k views

Users home path in a bash script

I'm writing a bash-script that will be run as a cron job everyday. Very basic, I was wanting to change the wallpaper daily. I have mint-14 with mate. The thing I'm getting caught up on right now ...
1
vote
3answers
6k views

Get Return value of sub shell script

I have a shell script, where i start a new .sh script. Now I want to save the return value of the sub script in a variable. I found this script: #!/bin/bash r=1 while [[ r -ne 0 ]] do r=`vx $* < ...
-1
votes
2answers
6k views

Bash -Extract characters from string

Suppose I have the following variable $string = 'cat somecommandhere' Now the output of the above cat command is test="Thanks serverfault" I want to extract the value between the quotes and ...
1
vote
3answers
339 views

Best tool for monitoring server usage

I was just wondering what the best tools for retracing logs and what logs I should be checking to work out what's going wrong with a server, or what the client has executed to get the server into it's ...
2
votes
3answers
100 views

Ctrl+W in vi[m] to erase to whitespace

I want to mimic the functionality of bash readline in vim. Is there an easy way to do that? Currently in vim Ctrl+W "cuts" from cursor back to special char or whitespace. ie. In bash Ctrl+W "cuts" ...
6
votes
2answers
18k views

bash shell - ssh remote script capture output and exit code?

I wish to use shell to invoke a script on a remote server. I would like to capture the output of that script (its logging messages) and the exit code it returns. If I do this: ssh user@server ...
3
votes
2answers
526 views

echo string >> file does not work

I wrote the following script: for filename in `find . -name '*'.cpp | grep $IN_REGEX | grep -v $OUT_REGEX` do echo "Output file is $OUTPUT_FILE" count=`git log --pretty=format: --name-only ...
2
votes
2answers
637 views

find all cpp files and list only those files with names that match a pattern (regex) does not work

I have a whole repository of files and I am trying to get a list of files that match a certain criteria. For example, let us say that I want to take all the files that have the string foo1 and ...
1
vote
2answers
90 views

How to print find match as well as run an -exec

I was wanting to run a find and then execute a script on each match; however, I was wanting to print the name of the matched file above the output from each exec. How can I produce the following ...
2
votes
2answers
1k views

Use of quotes in GNU grep regular expressions

I could see that certain patterns in the GNU Grep can be enclosed within brackets and certain others need not be. For example, matching the beginning of a word works only if it is enclosed within ...
2
votes
1answer
514 views

Find Directory and copy another directory to found directory?

I'm attempting to find a directory A that could be in a variable location and copy another directory B into the found directory. So, I'd like to find directory A, and copy Directory B into Directory ...
4
votes
4answers
1k views

Using GNU Parallel With Split

I'm loading a pretty gigantic file to a postgresql database. To do this I first use split in the file to get smaller files (30Gb each) and then I load each smaller file to the database using GNU ...
4
votes
0answers
80 views

Combing head and tail in a single call via pipe [duplicate]

On a regular basis, I am piping the output of some program to either head or tail. Now, suppose that I want to see the first AND last 10 lines of piped output, such that I could do something like ...
2
votes
2answers
858 views

How to pass the output of a script to a command like ls without the output being split?

Suppose I have a script like this: #!/bin/bash printf '%q\n' "b c" Executing the script prints: b\ c on the commandline. Now, being in a directory which contains a file named b c I want to pass ...
3
votes
1answer
949 views

for loop in bash function

I recently wrote the following bash function: makeaudiobook () { count=1 almbumartist=$2 for f in $1; do preprocess $f > $f-preprocessed text2wave $f-preprocessed -o $f.wav ...
3
votes
2answers
439 views

In an interactive shell why would anyone set IFS to something other than the default?

Sometimes when writing script it can be necessary to set IFS(the internal field separator) to something other than the default, e.g. to change the way $* will be expanded. While this is totally ...
4
votes
1answer
243 views

Where is the cron timer

Is there any way to access the cron timers and get the seconds left until the next execution of scripts in the crontab or maybe the seconds since the last?