Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

learn more… | top users | synonyms (2)

8
votes
3answers
1k views

Creating a Bash script that will execute a program and if it exits without crashing, will run it again

I'd like to know the easiest way of creating a bash script that will run an app in MacOSX (it's a UNIX based system so I assumed this was the right place). wait for the program to exit. If the exit ...
8
votes
5answers
2k views

How to run part of a script with reduced privileges?

I have the following problem: On every machine running Postgresql there is a special user postgres. This user has administrative access to the database server. Now I want to write a Bash script that ...
8
votes
4answers
5k views

What's the easiest way to find an unused local port?

What's the easiest way to find an unused local port? Currently I'm using something similar to this: port=$RANDOM quit=0 while [ "$quit" -ne 1 ]; do netstat -a | grep $port >> /dev/null if ...
8
votes
3answers
6k views

find -exec in bash script with variable expansion

I'm trying to run a command similar to the one below in a bash script. It should search through all subfolders of $sourcedir and copy all files of a certain type to the root level of $targetdir. ...
8
votes
6answers
8k views

Find biggest files or directories

Which command will print the sizes of all files and directories in the tmp directory (including hidden ones) and sort them by sizes from largest to smallest in human readable format (e.g. 2 GB)? The ...
8
votes
4answers
309 views

Do you prefer bash scripts or aliases for shortcuts?

I have a ~/bin directory (which is on my PATH) where I store a lot of little 1 or 2 line scripts. Some of them just cd into a directory and run a command on a file, like vim or something. But I also ...
8
votes
1answer
1k views

Suppress output unless non-zero exit code

What's the best way to suppress output (stdout and stderr) unless the program exits with a non-zero code? I'm thinking: quiet_success() { file=/tmp/suppressed if ! ( "$@" > "$file" ...
8
votes
3answers
145 views

POSIX compliant way to work with a list of filenames possibly with whitespace

I have seen Bash scripting guides suggesting the use of array for working with filenames containing whitespace. DashAsBinSh however suggests that arrays are not portable so I am looking for a POSIX ...
8
votes
0answers
209 views

how to get exit status from the command before the last [duplicate]

I have solaris machine ( solaris 10 ) please Look on the following simple commands (haconf -makerw | grep -iq "Cluster already writable") # haconf -makerw VCS WARNING V-16-1-10364 Cluster ...
7
votes
7answers
786 views

Slick one-liner to convert a list like “1: 2, 3, 4, 5” to “1.2, 1.3, 1.4, 1.5”

Let's say I have a file that looks something like this: 23: a, b, c, d 24: b, d, f 25: c, g and I want to get output like this: 23.a 23.b 23.c 23.d 24.b 24.d 24.f 25.c 25.g Of course it's not ...
7
votes
2answers
493 views

What does this ${@:2} mean in shell scripting

I see this in a shell script. variable=${@:2} What is it doing?
7
votes
7answers
2k views

cron Vs. sleep - which is the better one in terms of efficient cpu/memory utilization?

The Case: I need to run some commands/script at certain intervals of time and for this I have two options - 1. set up a cron-job or, 2. implement a loop with sleep in the script itself. Question: ...
7
votes
7answers
2k views

Bash - Continuous String Manipulation

#!/bin/bash FILE="$(basename "$1")" FILE="${FILE/%.jpeg/.jpg}" Is there anyway to glue these two lines together into a one-liner?
7
votes
5answers
412 views

List out strings which are substrings of other strings in the list

I have a list of names like so: dog_bone dog_collar dragon cool_dragon lion lion_trainer dog I need to extract out names that appear in other names like so: dragon lion dog I looked through the ...
7
votes
3answers
2k views

which shell will sudo use to execute a shell script without the shebang line

My environment is Ubuntu 12.04 LTS, and the sudo version is 1.8.3p1. First I login as a normal user: $ whoami fin $ cat /etc/passwd | grep -i "root\|fin" root:x:0:0:root:/root:/bin/bash ...
7
votes
5answers
26k views

How can I monitor all outgoing requests/connections from my machine?

My machine is a server so I want to ignore connections being made to my server (e.g. when someone visits my website). I want to see only connections/requests being made by my server to other places. ...
7
votes
3answers
2k views

Use & (ampersand) in single line bash loop

I have been using this command successfully, which changes a variable in a config file then executes a Python script within a loop: for((i=114;i<=255;i+=1)); do echo $i > numbers.txt;python ...
7
votes
3answers
2k views

What's the difference between Ctrl-Z and kill -STOP?

When I run a command (make on a large project) from the shell, I can type Ctrl-Z to stop the process and return to the shell. Subsequently, I can run fg to continue the process. I'm trying to write a ...
7
votes
1answer
631 views

Bash script doesn't read input

I have a script that is suppose to run a command in the background, and it does that. The problem is that when the script comes upon a read command, it doesn't pause and accept input. Here it is: ...
7
votes
2answers
7k views

Remove end of line characters from stdout? Multiple lines into a single line

I have a script which outputs the following text. This is the output from a Netopia 2210-02 ADSL2 modem. ADSL Line State: Up ADSL Startup Attempts: 1 ADSL Modulation: DMT ADSL Data ...
7
votes
5answers
4k views

Compare two files with first column and remove duplicate row from 2nd file in shell script

I will ask my question with an example. I have 2 files: File #1: 118D FC300_R5_TP FX.B 32775 2112 6 2038 6 2112 0 118E FC300_R5_TP FX.B 32775 2136 7 ...
7
votes
6answers
13k views

Show sum of file sizes in directory listing

The Windows dir directory listing command has a line at the end showing the total amount of space taken up by the files listed. For example, dir *.exe shows all the .exe files in the current ...
7
votes
3answers
8k views

How do I append text to the beginning and end of multiple text files in Bash?

I have a directory full of text files. My goal is to append text to the beginning and end of all of them. The text that goes at the beginning and end is the same for each file. Based on code I got ...
7
votes
4answers
3k views

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func ...
7
votes
2answers
542 views

Shell Script for going through a dir recursively and chmodding based on conditions of file type

Can anyone point me to either code or a tutorial for writing a shell script that can recursively go through an entire directory structure (starting at the current working directory, or given an ...
7
votes
3answers
1k views

Optimizing a `while` loop

I have created a mini script to reboot my Raspberry Pi upon the push of a button. The script simply uses wiringPi (gpio command) to set pin 0 (pin 17 in Raspberry Pi standard numbering order) to ...
7
votes
2answers
5k views

How can I remove an element from an array completely?

unset array[0] removes the element but still if I do echo ${array[0]} I get a null value moreover there are other ways of doing this but if an element of an array contains spaces like below ...
7
votes
2answers
401 views

Possible to use brace (permutation) and array expansion simultaneously?

Bash's {} brace expansion syntax allows for creating easy permutations # echo {b,c,d}{a,e,i,o,u} ba be bi bo bu ca ce ci co cu da de di do du However it's not clear to me if/how it's possible to ...
7
votes
1answer
3k views

Trap, ERR, and echoing the error line

I'm trying to create some error reporting using a Trap to call a function on all errors: Trap "_func" ERR Is it possible to get what line the ERR signal was sent from? The shell is bash. If I do ...
7
votes
2answers
1k views

XFCE or pure X11 commands, toggle compositing immediately without restarting X

How can i disable compositing via the command line? I need to disable temporarily for some games, like Nexuiz, for use in a wrapper script to toggle compositing status.
7
votes
1answer
362 views

ftp and shebang

Is there a way to shebang-ify ftp and write small ftp scripts? For example: #!/usr/bin/ftp open 192.168.1.1 put *.gz quit Any thoughts?
7
votes
1answer
249 views

“w | tail” chops text at right margin (78 characters)

Summary: w | tail chops rows after 78th column. When I run the "w" command, it works as expected. However, when I pipe the output to "tail", it chops the output to 78 columns wide (truncates anything ...
7
votes
3answers
641 views

shell scripts are still working without #!(sha-bang line)

I am new to shell scripts and many books have written that use #!(sha-bang) line in starting of the script to invoke the interpreter .And this will invoke a new shell for script and do the ...
7
votes
2answers
767 views

Check bash/shell script dependencies

Is there a method/command to check for the dependencies of a bash script? In other words, a response to this question : Which libraries should a user install to run the script? I can do this ...
7
votes
3answers
1k views

Script to toggle setxkbmap

I would like a bash script that toggles between: setxkbmap se and setxkbmap us. Then my intentions are to map that script to CtrlEsc through the Keyboard > custom shortcuts. The point is that I ...
7
votes
1answer
1k views

Are dash, ash and sh script 100% compatible?

I wanted to know whether scripts written for dash, ash, and sh are 100% compatible? Are there any added features to dash or ash, or syntax change? From what I heard, ash is a direct descendant of ...
7
votes
1answer
2k views

Converting html table into ncurses-like ascii table?

I have some documentation in an html table. I want to convert the table to an ascii table, like you see when you get a mysql command line result back, like this: +------------+------------+ | header ...
7
votes
1answer
290 views

Parallel execution of bash script

I have a table URL_Experiment in my database (mySQL database). I have 2 million URL links in this table. For each URL, I am checking if some particular text is present in the URL and updating the ...
7
votes
4answers
14k views

printing colored text using echo

I know that for printing a colored text using echo, for example red color, the code is: echo -e "\e[1;31m This is red text \e[0m" and I know that in this example, 31 is code of red color and the ...
7
votes
4answers
4k views

Parallel execution of a program on multiple files

I have a small script that loops through all files of a folder and executes a (usually long lasting) command. Basically it's for file in ./folder/*; do ./bin/myProgram $file > ./done/$file ...
7
votes
3answers
11k views

How to set global environment variables at boot through a script, and have them available for an application that runs before login?

I have a service that runs at boot, and in that service it calls a bash script in the background that exports some environment variables. The problem I'm having is that those environment variables ...
7
votes
4answers
10k views

Copy/rename multiple files using regular expression (shell script)

I have a collection of files matching a pattern such as 'assignment02.cc', 'assignment02.h', 'assignment02.txt', etc. I would like to copy/rename these files into 'assignment03.cc', 'assignment03.h', ...
7
votes
2answers
369 views

Strip // Comments From Files

What's the best way to strip all code comments in a given directory? I'd like to strip out all // ... EOL comments, and /* blah \*/ (or /** ... \*/) comments as well. This is a PHP project, and I'd ...
7
votes
2answers
326 views

Cache all gpg subkey passwords at once? Prevent need for multiple gpg password entry?

Can I enter my gpg password just once and unlock all my sub keys (signing, decryption, authentication)? At the moment, I need to enter my gpg password three times (for signing, for decryption, for ...
7
votes
4answers
2k views

redirect and log script output

I am trying to tidy up the following snippets, design goals are to log all output from a script, and should not be a wrapper. Less lines are better. I don't care about user inputs (at this stage), ...
6
votes
10answers
1k views

Copy only Specific text of a file to another

I have a file abc.txt the contents are <classpathentry kind="src" path="Sources"/> <classpathentry kind="con" path="WOFramework/ERExtensions"/> <classpathentry kind="con" ...
6
votes
5answers
3k views

How to create a testcolor.sh like the following screenshot?

I found this image on the internet, but don't know how was it made, Can anyone provide a sample on that? I just would like to have a color table as well.
6
votes
8answers
5k views

Can shell variable include - character?

I am not able to use - in variables in shell. Is there a way to be able to use it, because I have one script which depends on such named variables: $export a-b=c -bash: export: `a-b=c': not a valid ...
6
votes
4answers
1k views

How to run parallel processes and combine outputs when both finished

I have a bash shell script in which I pipe some data through about 5 or 6 different programs then the final results into a tab delimited file. I then do the same again for a separate similar dataset ...
6
votes
3answers
4k views

How to run a script from another path, and know the script's path?

I need to run a script that access a file in the same path it is located. For example: I have the script in /home/me/folder/script.sh, and this script will access the file /home/me/folder/myfile. As ...