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

learn more… | top users | synonyms (2)

13
votes
2answers
806 views

running script with “. ” and with “source ”

I was wondering if the following two ways of running a bash script are equivalent? . ./myScript.sh source myScript.sh Are they both running the content of the script instead of running the script, ...
13
votes
3answers
459 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
3answers
16k 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 ...
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 ...
13
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 ...
12
votes
6answers
11k 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
639 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
507 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
15k 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
6answers
5k 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 ...
12
votes
2answers
663 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
2k 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
957 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
5answers
4k views

How can I get bash to exit on backtick failure in a similar way to pipefail?

So I like to harden my bash scripts wherever I can (and when not able to delegate to a language like Python/Ruby) to ensure errors do not go uncaught. In that vein I have a strict.sh, which contains ...
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
19k 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
771 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
363 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
0answers
354 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
5answers
3k views

Bash script to get ASCII values for alphabet

How do I get the ASCII value of the alphabet? For example, 97 for a?
11
votes
4answers
9k 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 ...
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
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
7answers
645 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
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
725 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
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
4answers
11k 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 ...
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 ...
11
votes
5answers
9k 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: ...
10
votes
7answers
2k views

bash: test if $WORD is in set

I am looking for a construct in bash, to decide if a variable $WORD is one of defined words. I need something like this: if "$WORD" in dog cat horse ; then echo yes else echo no fi does ...
10
votes
3answers
3k 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?
10
votes
3answers
3k 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
4answers
5k 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. ...
10
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 ...
10
votes
3answers
2k 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
4answers
3k 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 ...
10
votes
2answers
17k 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 ...
10
votes
3answers
3k 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 ...
10
votes
2answers
1k 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 ...
10
votes
1answer
7k 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 ...
10
votes
1answer
3k views

Are there naming conventions for variables in shell scripts?

Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables ...
10
votes
7answers
1k views

Test if multiple variables are set

I'd like to make sure that at a certain point of a script, after sourceing a configuration file, several variables are set and, if they are not, to stop execution, telling the user about the missing ...
10
votes
2answers
3k views

Regular expression in bash script

This is my first time bash scripting so I'm probably making an easy mistake. Basically, I'm trying to write a script that gets the groups of a user, and if they are in a certain group, it will log ...
10
votes
2answers
208 views

How to start tailing a file that has not been yet created

I use tail to monitor the progress of jobs that I know will write their progress to disk. Almost always, I know which file they will create before they start running (the jobs are dispatched by a ...
10
votes
3answers
306 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, ...
9
votes
4answers
849 views

Bash syntax error when “else” follows an empty “then” clause

Why would following script not execute, but give a syntax error of else: LOGS3_DIR=~/logs if [ -d "$LOGS3_DIR" ]; then cd cd "$LOGS3_DIR" echo "$LOGS3_DIR" for filename in `find "." -mtime 1 ...
9
votes
2answers
2k 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 ...
9
votes
4answers
13k views

How to read from two input files using while loop

I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB. FileA: [jaypal:~/Temp] cat ...
9
votes
1answer
631 views

How do you omit “./” when running scripts on current directory

On some tutorials over the internet they can run a script on the current directory without doing: ./script How do you omit this? I am using CentOS 5.6 x64. I want to run it this way script