Questions tagged [shell-script]

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

Shell scripting is often considered a simple domain-specific programming language. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Further reading

16409 questions
213
votes
15 answers

How do I write a retry logic in script to keep retrying to run it upto 5 times?

I want to write logic in shell script which will retry it to run again after 15 sec upto 5 times based on "status code=FAIL" if it fails due to some issue.
183
votes
3 answers

Shell scripting: -z and -n options with if

I have a shell script where we have following lines if [ -z "$xyz" ] and if [ -n "$abc" ], but I am not sure what their purpose is. Can anyone please explain?
user3173953
  • 1,939
73
votes
5 answers

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 the directory, and cd out to continue onto the next…
46
votes
1 answer

Why the "-" in the "#! /bin/sh -" shebang?

#! /bin/sh - Is (or at least was) often the recommended shebang to have a script interpreted by /bin/sh (or #! /bin/bash - for bash, #! /bin/ksh - for ksh, etc). Why not just #! /bin/sh or #!/bin/sh? What's that - for?
42
votes
1 answer

How was I able to access the 14th positional parameter using $14 in a shell script?

I was reading about positional parameters in Unix and then I found this info: The shell allows a command line to contain at least 128 arguments; however, a shell program is restricted to referencing only nine positional parameters, $1 through $9,…
39
votes
2 answers

what is >> symbol and >& in unix/Linux?

I have a CRONTAB entry as below. Can someone tell me what the below statement is exactly doing? 1 0 * * * /vol01/sites/provisioning/MNMS/45627/45627.sh1 >> /vol01/sites/provisioning/MNMS/45627/output/cron.log 2>&1
user3475
  • 503
38
votes
8 answers

Convert underscore to PascalCase, ie UpperCamelCase

If I have a string that looks like this: "this_is_the_string" Inside a bash script, I would like to convert it to PascalCase, ie UpperCamelCase to look like this: "ThisIsTheString" I found that converting to lowerCamelCase can be done like…
36
votes
14 answers

How to check directory is empty?

I have a requirement, if I execute a script ./123 with an arguments of empty path, say /usr/share/linux-headers-3.16.0-34-generic/.tmp_versions(this directory is empty). It should display "directory is empty" My code is: #!/bin/bash dir="$1" if […
27
votes
1 answer

What does ${!#} mean in Shell scripting?

Found this in an obfuscated malicious shell script, beginning with: ${!#}${*^} <<<... Could not find any reference to ${!#}, but when echo'd, it outputs -bash. Is this a secret referene to the running shell? Why there is an extra dash then? Thanks!
dz902
  • 403
26
votes
6 answers

How to execute a script located in the same directory as the current script?

Why are shell scripts are so hard to develop? In NodeJS I could simply do: require('./script') and it will always require script relative to the current script. But if I try that in shell/bash: ./script.sh it will look for script relative to cwd…
26
votes
5 answers

Find files newer than a day and copy

I am working on a script that will copy ONLY files that have been created within the last day off to another folder. The issue I am having is the script I have copies all of the files in the source directory instead of just the files less than a…
mac1234
  • 311
  • 1
  • 3
  • 5
25
votes
4 answers

Get the date of last month's last day in a shell script

How do I get the previous month end date, based on processing date? Examples: Processing date = 15jan2015 Expected date = 31dec2014, Processing date = 10feb2015 Expected date = 31jan2015
22
votes
4 answers

Is there a "reverse sudo"?

Let's say I have a script that will be executed on various machines with root privileges, but I want to execute certain commands within that script without root. Is that possible?
Maxim
  • 728
22
votes
1 answer

What does the unix 'pick' command do?

I went through a command called 'pick' in some Unix book, but didn't understand what it does exactly. Below is a sample pick command: pick abc.*
21
votes
2 answers

Concatenating string variable inside a for loop in the bash shell

#!/bin/bash names= find /home/devuser -name 'BI*' echo $names for name in {names[@]} do echo $name $var = $var$name done echo $var
jagan
  • 251
1
2 3
72 73