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

learn more… | top users | synonyms (2)

14
votes
3answers
18k views

Testing if a variable is empty in a shell script

I have seen the following technique used many times on many different shells, to test if a variable is empty: if [ "x$1" = "x" ]; then # Variable is empty fi Are there any advantages on using ...
14
votes
2answers
3k views

Rule for invoking subshell in Bash?

I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process. However, this doesn't seem to be the case. In Code ...
14
votes
4answers
835 views

keep duplicates out of $PATH on source

I have the following code that's source-d by my .shellrc PATH="${PATH}:${HOME}/perl5/bin" PATH="${PATH}:${HOME}/.bin" export PATH but if I make changes to other code and then source this file, my ...
13
votes
4answers
7k views

What is the meaning of $? in a shell script?

When going through one shell script, I saw the term "$?". What is the significance of this term?
13
votes
6answers
6k views

Best way to follow a log and execute a command when some text appears in the log

I have a server log that outputs a specific line of text into its log file when the server is up. I want to execute a command once the server is up, and hence do something like the following: tail -f ...
13
votes
5answers
528 views

Stateful bash function

I’d like to implement a function in Bash which increases (and returns) a count with every call. Unfortunately this seems non-trivial since I’m invoking the function inside a subshell and it ...
13
votes
3answers
5k 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
3answers
471 views

When does `ls -s` print “0”

Of course, the standard way of testing if a file is empty is with test -s FILE, but one of our clients have received a script containing tests like this: RETVAL=`ls -s ./log/cr_trig.log | awk '{print ...
13
votes
4answers
3k views

How to make bash abort the execution of a script on syntax error?

To be on safe side, I'd like bash abort the execution of a script if it encounters a syntax error. To my surprise, I can't achieve this. (set -e is not enough.) Example: #!/bin/bash # Do exit on ...
12
votes
5answers
4k views

Bash script to get ASCII values for alphabet

How do I get the ASCII value of the alphabet? For example, 97 for a?
12
votes
4answers
10k views

Why doesn't “sudo su” in a shell script run the rest of the script as root?

A sample script can be as below: #!/bin/bash sudo su ls /root When using ./test.sh as the normal user, instead run ls as super user and exit, it switches to root; and when I logout, it executes ls ...
12
votes
7answers
12k views

How to compare to floating point number in a shell script

I want to compare two floating point numbers in a shell script. The following code is not working: #!/bin/bash min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min
12
votes
4answers
658 views

Recursively create directories for all letters

I want to create a directory in such a way that I need to label the directories from a to z. Inside each of these directories, I need to create sub-directories so that they are labelled as aa, ab etc. ...
12
votes
3answers
2k views

Comparing two strings in Bash

I have the following if block in my bash script: if [ ${PACKAGENAME} -eq kakadu-v6_4-00902C ]; then echo "successfully entered if block!!" fi The script execution is not entering my if block even ...
12
votes
4answers
17k views

How to delete a file on remote machine via SSH by using a Shell Script?

I am writing a Shell Script where I have to delete a file on a remote machine via a Shell Script. Flow manually: ssh username@domain.com .. then at domain: cd ./some/where rm some_file.war How ...
12
votes
2answers
505 views

How does awk '!a[$0]++' work?

This one-liner removes duplicate lines from text input without pre-sorting. For example: $ cat >f q w e w r $ awk '!a[$0]++' <f q w e r $ The original code I have found on the internets ...
12
votes
6answers
1k views

Portable way to get script's absolute path?

What is a portable way for a (zsh) script to determine its absolute path? On Linux I use something like mypath=$(readlink -f $0) ...but this is not portable. (E.g., readlink on darwin does not ...
12
votes
2answers
713 views

When to use a semi-colon between environment variables and a command

Can anyone explain why the semi-colon is necessary in order for the LANG to be seen as updated by bash? Doesn't work: > LANG=Ja_JP bash -c "echo $LANG" en_US Works: > LANG=Ja_JP ; bash -c ...
12
votes
3answers
5k views

Variable as command; eval vs bash -c

I was reading a bash script someone made and I noticed that the author doesn't use eval to evaluate a variable as a command The author used bash -c "$1" instead of eval "$1" I assume using eval ...
12
votes
1answer
11k views

How can I execute local script on remote machine and include arguments?

I have written a script that runs fine when executed locally: ./sysMole -time Aug 18 18 The arguments "-time", "Aug", "18", and "18" are successfully passed on to the script. Now, this script is ...
12
votes
2answers
439 views

When running a shell script, is it possible to pass specific positional parameters without having to enter them all in order?

For example, if I have script ./foo that takes 10 parameters, and I only want to pass the 8th parameter. The only way I know how to do this currently is: ./foo '' '' '' '' '' '' '' 'bar' Is there ...
12
votes
1answer
1k views

When is double-quoting necessary?

The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of ...
12
votes
4answers
13k views

How can I use bash's if test and find commands together?

I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command. The log files are stored in this format: /var/log/crashes/app-2012-08-28.log ...
12
votes
2answers
2k views

How to safely pass variables to root-enabled scripts?

This question is totally general and not only applicable to my situation, but... I have a small busybox appliance where I want a non-root user to be able to execute a particular script with root ...
12
votes
3answers
370 views

How do I wait on a program started in another shell

I have a program that does a large a amount of work (takes about 4-5 hours) that gets started by cron when all the data it works with becomes available. Sometimes, when I am waiting for it to finish, ...
12
votes
3answers
23k views

Shell script fails: Syntax error: “(” unexpected

I've been working on a script that automates setting up a development environment for Raspberry Pi development (step by step details that work are here). The script is linked in that article but ...
12
votes
1answer
900 views

How does a fork bomb work?

WARNING DO NOT ATTEMPT TO RUN THIS ON A PRODUCTION MACHINE In reading the Wikipedia page on the topic I generally follow what's going on with the following code: :(){ :|:& };: excerpt of ...
12
votes
3answers
386 views

When to use redirection to stderr in shell scripts

I know that well-behaved utilities like grep output "normal" messages to stdout, and error messages to stderr. $ grep '^foo' file1 file2 file1:foo grep: file2: No such file or directory When I'm ...
12
votes
5answers
12k views

Check if folder is a mounted remote filesystem

What is the best way (reliable, portable, etc.) to check if a given folder is on a mounted remote (nfs) filesystem within a shell script? I am looking for a command that would look like: ...
12
votes
0answers
356 views

Should I use $* or $@? [duplicate]

Which one of $* or $@ is better to use (in array syntax,command line parameters, etc.) to avoid any bugs/problems in code? Or it does not make any difference?
11
votes
2answers
1k views

find and remove duplicates in a directory

I have a directory with multiple img files and some of them are identical but they all have different names. I need to remove duplicates but with no external tools only with a bash script. I'm a ...
11
votes
3answers
4k views

How to “send” variable to sub-shell?

I run the following script: VAR="Test" sh -c 'echo "Hello $VAR"' But I get : # ./test.sh Hello How can I "send" the variable VAR of my script to the shell created with sh -c?
11
votes
5answers
5k views

How can I use environment variables in my shebang?

I have a Python script that need to be run with a particular python installation. Is there a way to craft a shebang so that it runs with $FOO/bar/MyCustomPython?
11
votes
4answers
6k views

How to define and load your own shell function in zsh

I am having a hard time defining and running my own shell functions in zsh. I followed the instructions on the official documentation and tried with easy example first, but I failed to get it work. ...
11
votes
7answers
872 views

Shortest way to extract last 3 characters of base (minus suffix) filename

I'm trying to set a variable in an sh script to the last 3 characters of the base name of a file (by base name I mean without the path and without the suffix). I've succeeded in doing this but, purely ...
11
votes
4answers
4k views

Executing user defined function in a find -exec call

I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1). The following code doesn't yield any result: function foo { echo "Hello World" } find somedir -exec ...
11
votes
2answers
20k views

How do I delete the first n lines of an ascii file using shell commands?

I have multiple files that contain ascii text information in the first 5-10 lines, followed by well-tabulated matrix information. In a shell script, I want to remove these first few lines of text so ...
11
votes
2answers
6k views

find n most frequent words in a file

I want to find, say, 10 most common word in a text file. Firstly, solution should be optimized for keystrokes (in other words - my time). Secondly, for the performance. Here is what I have so far to ...
11
votes
1answer
763 views

How to generate a lot of (blank) images files?

For a test, I need to quickly create 1700 jpg-images (even blank) in a defined size 1000x706. I wonder if we could do this with a shell script or a program (like ImageMagick). I wish they called ...
11
votes
3answers
4k views

How can I receive top-like CPU statistics from the shell?

I'm trying to get an accurate read of my used CPU (in percent) from top. This is the command I'm running for testing: top -n1 | awk '/Cpu\(s\):/ {print $2}' This returns: 10.7%us, Which is the ...
11
votes
5answers
8k views

#!/bin/bash - no such file or directory

I've created a bash script but when I try to execute it, I get #!/bin/bash no such file or directory I need to run the command: bash script.sh for it to work. How can I fix this?
11
votes
2answers
2k views

Simplest way to comment/uncomment certain lines using command line

Is there a way to comment/uncomment a shell/config/ruby script using command line? for example: $ comment 14-18 bla.conf $ uncomment 14-18 bla.conf this would add or remove # sign on bla.conf on ...
11
votes
4answers
3k views

Is there a JavaScript shell?

Recently I've been working with JS and I'm very enthusiastic about this language. I know that there is node.js for running JS at server side, but is there a shell that uses JS as a scripting language? ...
11
votes
8answers
1k views

Detect init system using the shell

This may have more to do with detecting operating systems, but I specifically need the init system currently in use on the system. Fedora 15 now uses systemd, Ubuntu uses Upstart, while others use ...
11
votes
3answers
1k views

Are free shell accounts safe?

I am trying to identify how safe the free shell accounts provided by the likes of shellmix.com are. I have never used one myself but want to suggest it to someone interested in learning shell ...
10
votes
2answers
3k views

Fastest way to concatenate files

I've got 10k+ files totaling over 20GB that I need to concatenate into one file. Is there a faster way than cat input_file* >> out ? The preferred way would be a bash command, Python is ...
10
votes
3answers
23k views

How to append multiple lines to a file with bash

I am writing a bash script to look for a file if it doesn't exist then create it and append this to it: Host localhost ForwardAgent yes So "line then new line 'tab' then text" I think its a ...
10
votes
3answers
4k views

How to sort the string which combined with string + numeric using bash script?

This is the data what I want to sort. But sort treats the numeric to string, the data it no sorted as I expected. /home/files/profile1 /home/files/profile10 /home/files/profile11 ...
10
votes
3answers
3k views

Fix or alternative for mktemp in OS X

I'm looking at a bash script someone else wrote that uses mktemp: TEMP=`mktemp --directory` However, this line does not work on my machine (OS X 10.6). How would I fix this line so that it is ...
10
votes
3answers
17k views

File extensions for unix shell scripts

On wikipedia, the article for .sh says: For the .sh file extension type, see Bourne shell. How about other unix shells? I know that the shebang is used inside the file to indicate an ...