Questions tagged [scripting]

A script is a sequence of commands or instructions that are executed by an interpreter program rather than compiled into a standalone executable program.

Script are supposed to be smaller program, easy to write and useful for specific and daily tasks.

Further reading

4427 questions
646
votes
4 answers

Using "${a:-b}" for variable assignment in scripts

I have been looking at a few scripts other people wrote (specifically Red Hat), and a lot of their variables are assigned using the following notation VARIABLE1="${VARIABLE1:-some_val}" or some expand other variables VARIABLE2="${VARIABLE2:-`echo…
Justin Garrison
  • 7,359
  • 4
  • 20
  • 21
635
votes
24 answers

Repeat a Unix command every x seconds forever

There's a built-in Unix command repeat whose first argument is the number of times to repeat a command, where the command (with any arguments) is specified by the remaining arguments to repeat. For example, % repeat 100 echo "I will not automate…
dreeves
  • 6,499
  • 3
  • 15
  • 7
319
votes
5 answers

How can I get the current working directory?

I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd. It just prints pwd how do I get the current working directory into a variable?
user104976
256
votes
11 answers

Allow setuid on shell scripts

The setuid permission bit tells Linux to run a program with the effective user id of the owner instead of the executor: > cat setuid-test.c #include #include int main(int argc, char** argv) { printf("%d", geteuid()); …
Michael Mrozek
  • 91,316
  • 38
  • 238
  • 232
226
votes
9 answers

Looping through files with spaces in the names?

I wrote the following script to diff the outputs of two directores with all the same files in them as such: #!/bin/bash for file in `find . -name "*.csv"` do echo "file = $file"; diff $file /some/other/path/$file; read…
Amir Afghani
  • 7,083
  • 11
  • 26
  • 23
197
votes
8 answers

Automated ssh-keygen without passphrase, how?

I would like to make an automated script that calls ssh-keygen and creates some pub/private keypairs that I will use later on. In principle everything works fine with.... ssh-keygen -b 2048 -t rsa -f /tmp/sshkey -q ...except that it asks me for the…
humanityANDpeace
  • 13,722
  • 13
  • 61
  • 107
190
votes
6 answers

how can I add (subtract, etc.) two numbers with bash?

I can read the numbers and operation in with: echo "First number please" read num1 echo "Second number please" read num2 echo "Operation?" read op but then all my attempts to add the numbers fail: case "$op" in "+") echo num1+num2;; "-") …
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
168
votes
4 answers

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 designed to be executed on a remote machine but, from…
AllenD
  • 2,397
  • 4
  • 17
  • 14
151
votes
3 answers

Have backticks (i.e. `cmd`) in *sh shells been deprecated?

I've seen this comment many times on Unix & Linux as well as on other sites that use the phrasing "backticks have been deprecated", with respect to shells such as Bash & Zsh. Is this statement true or false?
slm
  • 363,520
  • 117
  • 767
  • 871
141
votes
3 answers

Pass command line arguments to bash script

I am new to bash script programming. I want to implement a bash script 'deploymLog', which accepts as input one string argument(name). [root@localhost Desktop]# ./deploymLog.sh name here I want to pass the string argument(name) through command line…
chinchu
141
votes
11 answers

Script to change current directory (cd, pwd)

I want to run a script to simply change the current working directory: #!/bin/bash cd web/www/project But, after I run it, the current pwd remains unchanged! How can I do that?
Sony Santos
  • 1,513
  • 2
  • 10
  • 7
132
votes
6 answers

Confusing use of && and || operators

I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. I've read where they can be used in statements such as: if […
josh-cain
  • 1,781
  • 3
  • 16
  • 13
126
votes
3 answers

What does it mean to have a $"dollarsign-prefixed string" in a script?

I just saw this in an init script: echo $"Stopping Apache" What is that dollar-sign for? My research so far: I found this in the bash manual: extquote If set, $'string' and $"string" quoting is performed within ${parameter} expansions enclosed in…
Ed Brannin
  • 1,363
  • 2
  • 9
  • 6
126
votes
7 answers

How to get whole command line from a process?

How can I get the command arguments or the whole command line from a running process using its process name? For example this process: # ps PID USER TIME COMMAND 1452 root 0:00 /sbin/udhcpc -b -T 1 -A 12 -i eth0 -p…
Michael
  • 1,583
  • 3
  • 13
  • 19
119
votes
13 answers

Replace environment variables in a file with their actual values?

Is there an easy way to substitute/evaluate environment variables in a file? Like let's say I have a file config.xml that contains: instanceId $INSTANCE_ID
1
2 3
99 100