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

learn more… | top users | synonyms (2)

15
votes
2answers
40k views

sudo: unable to execute ./script.sh: no such file or directory

I'm stumped. I have a script in my /home directory which is executable: [user@server ~]$ ll total 4 -rwx------ 1 user user 2608 Jul 15 18:23 qa.sh However, when I attempt to run it with sudo it ...
15
votes
2answers
2k views

Are quotes needed for local variable assignment?

Can I safely omit quotes on the right side of a local assignment? function foo { local myvar=${bar} stuff() } I'm mainly interested in bash, but any info on corner cases in other shells are ...
15
votes
4answers
760 views

What is the difference between '$ . foo' and '$ ./foo'? [duplicate]

t is a simple script What is different between two case below : $ . t and $ ./t
15
votes
4answers
39k 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 ...
14
votes
4answers
13k 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?
14
votes
5answers
1k views

Compress a large number of large files fast

I have about 200 GB of log data generated daily, distributed among about 150 different log files. I have a script that moves the files to a temporary location and does a tar-bz2 on the temporary ...
14
votes
3answers
10k 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?
14
votes
3answers
710 views

Prepare answers for questions of a command

Let's say I want to write a shell script that executes just one command. But this command is poorly designed. It doesn't offer any command line options; instead it asks some questions and waits for ...
14
votes
5answers
1k views

Is this a bug in bash? `return` doesn't quit function if called from a pipe

I'm been having some weird problems with bash lately. While trying to simplify my script, I came up with this small piece of code: $ o(){ echo | while read -r; do return 0; done; echo $?;}; o 0 $ o(){...
14
votes
3answers
2k views

Date validation in shell

I want to validate the below date format in shell script 2015-Jul-13 I'm using date -d "2015-Jul-13" +"%Y-%b-%d" but its giving error as date: invalid date '2015-Jul-13'
14
votes
8answers
36k views

gpg asks for password even with --passphrase

I expect the following command to extract the gpg file without asking for password: gpg --passphrase 1234 file.gpg But it asks for the password. Why? This also have the same behavior: gpg --...
14
votes
1answer
21k views

File descriptors & shell scripting

I am having a very hard time understanding how does one use file descriptors in shell scripts. I know the basics such as exec 5 > /tmp/foo So fd 5 is attached to foo for writing. exec 6 < /...
14
votes
1answer
427 views

Where are bash line continuations after && and || documented?

I have seen this construct in scripts a lot and used it myself, but it bothers me that I can't seem to find it in the documentation. Example: [ -f file1 ] && [ -f file2 ] && echo "...
14
votes
4answers
28k views

How can I suppress output from grep, so that it only returns the exit status?

I have the grep command. I'm searching for a keyword from a file, but I don't want to display the match. I just want to know the exit status of the grep.
14
votes
3answers
790 views

Differentiating between running and being sourced in a bash shell script?

Either what I'm asking here is extremely unorthodox/unconventional/risky, or my Google-fu skills just aren't up to snuff... In a bash shell script, is there any easy of way of telling if it is ...
14
votes
3answers
20k views

How do I concatenate all the files in a given directory in order of date, where I want the newest file on top?

And with the oldest file on bottom? Also, if I do this, is it also possible to strip out the redundant headers contained within each HTML file? I'm seeing myself concatenate a lot of HTML files up, ...
14
votes
9answers
1k views

Filter or pipe certain sections of a file

I have an input file with some sections the are demarcated with start and end tags, for example: line A line B @@inline-code-start line X line Y line Z @@inline-code-end line C line D I want to ...
14
votes
9answers
8k 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 ...
14
votes
4answers
6k 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 ...
14
votes
4answers
1k views

keep duplicates out of $PATH on source [duplicate]

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 ...
14
votes
2answers
2k views

Correct behavior of EXIT and ERR traps when using `set -eu`

I'm observing some weird behavior when using set -e (errexit), set -u (nounset) along with ERR and EXIT traps. They seem related, so putting them into one question seems reasonably. 1) set -u does ...
13
votes
8answers
10k views

Is there a unix command that gives the minimum/maximum of two numbers?

I was looking for a command to limit numbers read in from stdin. I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, ...
13
votes
4answers
968 views

How do I run a command and get its exit code in fewer characters?

I used to write my scripts like this: some_command while [ $? -ne 0 ] ; do sleep 1 some_command done It always irked me that I have to write some_command twice. Can I put it inside the loop ...
13
votes
7answers
2k views

bash if not multiple conditions without subshell?

I want to combine multiple conditions in a shell if statement, and negate the combination. I have the following working code for a simple combination of conditions: if [ -f file1 ] && [ -f ...
13
votes
5answers
2k views

Convert underscore to PascalCase, ie UpperCamelCase

If I have a string that looks like this: "this_is_the_string" Inside bash script, I would like to convert it to PascalCase, ie UpperCamelCase to look like this: "ThisIsTheString" I found that ...
13
votes
4answers
59k views

How can I pass a command line argument into a shell script?

I know that shell scripts just run commands as if they were executed in at the command prompt. I'd like to be able to run shell scripts as if they were functions... That is, taking an input value or ...
13
votes
5answers
45k 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 ...
13
votes
3answers
11k views

Get path of current script when executed through a symlink

I have a utility consisting of a couple directories with some bash scripts and supporting files that will be deployed to several machines possibly in a different directory on each machine. The ...
13
votes
2answers
767 views

Why does “bash -x” break this script?

I have a script which measures how long some command executes. It needs the "real" time command, meaning, a binary for example in /usr/bin/time (as the bash-built-in doesn't have the -fflag). Below, ...
13
votes
7answers
13k views

A bash function that takes argument like other languages?

I have a bash function to set the $PATH like this -- assign-path() { str=$1 # if the $PATH is empty, assign it directly. if [ -z $PATH ]; then PATH=$str; # if the $PATH does ...
13
votes
5answers
26k views

Binary to hexadecimal and decimal in a shell script

I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?
13
votes
2answers
707 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 ...
13
votes
1answer
28k views

how to set environment variables from a shell script? [duplicate]

I'm trying to set environment variable through a shell script. Here is the script with various techniques: #!/bin/sh echo "export SOMEPATH=/root/some" >> /root/.bash_profile echo "export PATH=$...
13
votes
4answers
31k views

Text between two tags

I wanna retrieve whatever is between these two tags – <tr> </tr> – from an html doc. Now I don't have any specific html requirements that would warrant for an html parser. I just plain ...
13
votes
7answers
52k views

Add lines to the beginning and end of the huge file

I have the scenario where lines to be added on begining and end of the huge files. I have tried as shown below. for the first line: sed -i '1i\'"$FirstLine" $Filename for the last line: sed -i ...
13
votes
2answers
49k views

Cron job to delete files older than 3 days

I need to remove files older than 3 days with a cron job in 3 different directories. (these 3 directories are children of a parent directory /a/b/c/1 & /a/b/c/2 & /a/b/c/3) Can this be done ...
13
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 ...
13
votes
1answer
2k 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 ...
13
votes
3answers
787 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 ...
13
votes
3answers
521 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 ...
12
votes
3answers
872 views

How to remove \n between the outputs of two echo commands?

I have a text file containing one file name in each line: 111_c4l5r120.png 123_c4l4r60.png 135_c4l4r180.png 147_c4l3r60.png 15_c4l1r120.png ... I want to convert it in this shape: 111_c4l5r120.png ...
12
votes
5answers
17k views

Is there a way to sum up the size of files listed?

This is the command I am using to list some files: find . -name \*.extract.sys -size +1000000c -exec ls -lrt {} \; -rw-r--r-- 1 qa1wrk15 test 1265190 Sep 29 01:14 ./var/can/projs/ar/rep/...
12
votes
2answers
4k 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 ...
12
votes
4answers
14k views

How do I handle switches in a shell script?

Are there some built-in tools that will recognize -x and --xxxx as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments ...
12
votes
4answers
55k views

Storing output of command in shell variable [duplicate]

I have an operation using cut that I would like to assign result to a variable var4=echo ztemp.xml |cut -f1 -d '.' I get the error: ztemp.xml is not a command The value of var4 never gets ...
12
votes
5answers
15k views

cd into all directories, execute command on files in that directory, and return to previous current directory

I'm attempting to write a script that will be run in a given directory with many single level sub directories. The script will cd into each of the sub directories, execute a command on the files in ...
12
votes
5answers
5k views

Get total duration of video files in a directory

I have a list of .ts files: out1.ts ... out749.ts out8159.ts out8818.ts How can I get the total duration (running time) of all these files?
12
votes
6answers
2k views

Using “reserved” codes for exit status of shell scripts

I recently came across this list of Exit Codes With Special Meanings from the Advanced Bash-Scripting Guide. They refer to these codes as being reserved and recommend that: According to the above ...
12
votes
4answers
3k 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
8k views

chmod + silent mode + how force exit code 0 in spite of error

Is it possible execute chmod and ignore error from command Example ( remark file.txt not exsist to show the example ) When I type chmod 777 file.txt I get error on the output chmod: cannot ...