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.

Filter by
Sorted by
Tagged with
647 votes
4 answers
492k views

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 ...
Justin Garrison's user avatar
635 votes
24 answers
703k views

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 ...
dreeves's user avatar
  • 6,499
320 votes
5 answers
1.2m views

How can I get the current working directory? [duplicate]

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 ...
user avatar
258 votes
11 answers
217k views

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 <stdio.h> #include <unistd.h> int ...
Michael Mrozek's user avatar
229 votes
9 answers
299k views

Looping through files with spaces in the names? [duplicate]

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 $...
Amir Afghani's user avatar
  • 7,113
198 votes
8 answers
245k views

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 ...
humanityANDpeace's user avatar
190 votes
6 answers
477k views

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: ...
Michael Durrant's user avatar
169 votes
4 answers
294k 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 ...
AllenD's user avatar
  • 2,407
151 votes
3 answers
34k views

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 ...
slm's user avatar
  • 364k
141 votes
11 answers
372k views

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's user avatar
  • 1,513
141 votes
3 answers
552k views

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 ...
user avatar
132 votes
6 answers
474k views

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'...
josh-cain's user avatar
  • 1,791
127 votes
3 answers
92k views

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&...
Ed Brannin's user avatar
  • 1,373
126 votes
7 answers
207k views

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/...
Michael's user avatar
  • 1,583
120 votes
13 answers
155k views

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: <property> <name>instanceId</name> <...
Robert Fraser's user avatar
115 votes
2 answers
361k views

su options - running command as another user

I was wondering how to run a command as another user from a script. I have the script's owner set as root. I also have the following command being run within the script to run the command as the ...
myusuf3's user avatar
  • 1,385
114 votes
5 answers
85k views

Does the shebang determine the shell which runs the script?

This may be a silly question, but I ask it still. If I have declared a shebang #!/bin/bash in the beginning of my_shell_script.sh, so do I always have to invoke this script using bash [my@comp]$...
jrara's user avatar
  • 2,069
112 votes
3 answers
158k 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.
jackass27's user avatar
  • 1,123
107 votes
10 answers
451k views

How to grep for same string but multiple files at the same time?

I have a set of log files that I need to review and I would like to search specific strings on the same files at once Is this possible? Currently I am using grep -E 'fatal|error|critical|failure|...
user53029's user avatar
  • 2,725
103 votes
6 answers
233k views

Using while loop to ssh to multiple servers

I have a file servers.txt, with list of servers: server1.mydomain.com server2.mydomain.com server3.mydomain.com when I read the file line by line with while and echo each line, all works as expected....
Martin Vegter's user avatar
98 votes
5 answers
206k views

Bash throws error, line 8: $1: unbound variable

I am trying to learn how to use getopts so that I can have scripts with parsed input (although I think getopts could be better). I am trying to just write a simple script to return partition usage ...
Timothy Pulliam's user avatar
89 votes
6 answers
268k views

How to exit a shell script if one part of it fails?

How can I write a shell script that exits, if one part of it fails? For example, if the following code snippet fails, then the script should exit. n=0 until [ $n -ge 5 ] do gksu *command* &&...
Weylyn's user avatar
  • 915
89 votes
1 answer
156k views

Easy command line method to determine specific ARM architecture string?

I'm trying to write a script which will determine actions based on the architecture of the machine. I already use uname -m to gather the architecture line, however I do not know how many ARM ...
Thomas Ward's user avatar
  • 2,610
87 votes
8 answers
41k views

Multiple arguments in shebang

I am wondering whether there is a general way of passing multiple options to an executable via the shebang line (#!). I use NixOS, and the first part of the shebang in any script I write is usually /...
Rastapopoulos's user avatar
78 votes
4 answers
60k views

$@ except the 1st argument

I need to write a shell script that runs in this way: ./myscript arg1 arg2_1 arg2_2 arg2_3 ....... arg2_# there is a for loop inside script for i in $@ However, as I know, $@ includes $1 up to $(...
user40780's user avatar
  • 1,841
75 votes
1 answer
73k views

shell find -delete "directory not empty" [duplicate]

I try to delete backup files on my Synology NAS older than 30 days. The files are in a directory which is created during download of the backup files from my webserver. After download, I want to check ...
grandd's user avatar
  • 869
74 votes
3 answers
28k views

Shebang line with `#!/usr/bin/env command --argument` fails on Linux

I've got a simple script: #!/usr/bin/env ruby --verbose # script.rb puts "hi" On my OSX box, it runs fine: osx% ./script.rb hi However, on my linux box, it throws an error linux% ./script.rb /usr/...
rampion's user avatar
  • 1,589
73 votes
2 answers
31k views

Copy image from clipboard to file

Is it possible to take an image from the clipboard and output it to a file (using X)? I can do this with text easily: $ xclip -selection c -o > file.text But when I try the above with an image ...
Lerp's user avatar
  • 863
70 votes
12 answers
111k 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, ...
Minix's user avatar
  • 5,745
70 votes
8 answers
80k views

AutoHotkey equivalent

Is there an equivalent product/method for AutoHotkey? For those that don't know, it's a product that allows you to program your mouse movements and keyboard. This allows me to "macro" certain ...
Meer Borg's user avatar
  • 916
64 votes
5 answers
196k views

How do I exit a script in a conditional statement?

I'm writing a bash script where I want to exit if the user is not root. The conditional works fine, but the script does not exit. [[ `id -u` == 0 ]] || (echo "Must be root to run script"; exit) I'...
Garrett Hall's user avatar
  • 5,131
63 votes
16 answers
264k views

Automatically run commands over SSH on many servers

There is a list of IP addresses in a .txt file, ex.: 1.1.1.1 2.2.2.2 3.3.3.3 Behind every IP address there is a server, and on every server there is an sshd running on port 22. Not every server is ...
LanceBaynes's user avatar
  • 39.4k
62 votes
1 answer
100k views

Convenient way to check if system is using systemd or sysvinit in BASH? [duplicate]

I am defining common bash files which I want to use across different distributions. I need a way to check if system is using systemd or sysvinit (/etc/init.d/). I need this so I run appropriate ...
ek9's user avatar
  • 2,875
61 votes
7 answers
181k views

Execute remote commands, completely detaching from the ssh connection

I have 2 computers, localpc and remoteserver. I need localpc to execute some commands on remoteserver. One of the things it needs to do is start a backup script that runs for a number of hours. I ...
LVLAaron's user avatar
  • 1,715
58 votes
2 answers
58k views

Create SSL certificate non-interactively [closed]

I want to silently, non interactively, create an SSL certificate. I.e., without get prompted for any data. The normal way I create the certificate would be: openssl req -x509 -nodes -days 7300 -...
TheNiceGuy's user avatar
57 votes
7 answers
128k views

File extensions for unix shell scripts [closed]

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 ...
Amelio Vazquez-Reina's user avatar
57 votes
4 answers
193k views

Bash multiplication and addition

for k in {0..49}; do a=$(($((2*$k))+1)); echo $a; done Hi, I need a simplified expression for the third line, maybe one that does not use command substitution.
AVS's user avatar
  • 773
57 votes
3 answers
144k views

Running a script during booting/startup; init.d vs cron @reboot

I am currently trying to understand the difference between init.d and cron @reboot for running a script at startup/booting of the system. The use of @reboot (this method was mentioned in this forum by ...
3kstc's user avatar
  • 4,616
56 votes
8 answers
114k views

Using xargs with input from a file

Say I have a file with the following bob john sue Now these directly corrospond to (in this case) URL pattern such as http://example.com/persons/bob.tar, john.tar, sue.tar. I would like to take ...
Josh K's user avatar
  • 3,866
54 votes
5 answers
27k views

Where are userspace programs supposed to save their logs?

I am writing a script that I want to run without privileges. I want the errors that the script encounters to be logged to some log file. I do not not have privileges to write one to /var/log. And I do ...
Lord Loh.'s user avatar
  • 1,926
51 votes
4 answers
28k views

How do I find the line number in Bash when an error occured?

How do you find the line number in Bash where an error occurred? Example I create the following simple script with line numbers to explain what we need. The script will copy files from cp $file1 $...
yael's user avatar
  • 12.6k
50 votes
7 answers
28k views

How to run ssh command until succeeded?

Sometimes I restart a device and need to ssh back in when it's ready. I want to run the ssh command every 5 seconds until the command succeeds. My first attempt: watch -n5 ssh [email protected] &&...
Philip Kirkbride's user avatar
50 votes
6 answers
18k views

Universal Node.js shebang?

Node.js is very popular these days and I've been writing some scripts on it. Unfortunately, compatibility is a problem. Officially, the Node.js interpreter is supposed to be called node, but Debian ...
StackExchange saddens dancek's user avatar
49 votes
4 answers
64k views

Bash shell script output alignment

My script: date echo -e "${YELLOW}Network check${NC}\n\n" while read hostname do ping -c 1 "$hostname" > /dev/null 2>&1 && echo -e "Network $hostname : ${GREEN}Online${NC}" || ...
pijaaa's user avatar
  • 501
49 votes
7 answers
32k views

What Unix commands can be used as a semaphore/lock?

I want to run multiple Bash shell scripts in parallel. However, I want to avoid race conditions. What Unix commands are truly atomic that I could use for this purpose, and how can I use them?
Larry Wang's user avatar
  • 2,635
48 votes
4 answers
60k views

How to make a temporary file in RAM?

I have a script that will pipe its output to |tee scriptnameYYMMDD.txt. After each cycle of the for loop in which the output is generated, I'll be reversing the file contents with tac ...
user208145's user avatar
  • 2,447
48 votes
3 answers
62k views

Using the setuid bit properly

I have a process that needs root privileges when run by a normal user. Apparently I can use the "setuid bit" to accomplish this. What is the proper way of doing this on a POSIX system? Also, how ...
goldilocks's user avatar
  • 86.6k
46 votes
7 answers
4k views

What's a good mnemonic for shell double vs. single quotes?

I don't do enough scripting to remember, without looking up, whether double or single quotes result in a Unix variable being substituted. I definitely understand what is going on. My question is does ...
Dave Potts's user avatar
46 votes
6 answers
184k views

How to capture error message from executed command?

I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and ...
Miguel Roque's user avatar
46 votes
3 answers
29k views

tmux script to launch several commands

How can I write the following in a bash script? tmux # Start tmux session. compass watch /path/to/project1/compass/ # Run the first process. Ctrl + B, " # Split the pane. compass watch /path/to/...
GTS Joe's user avatar
  • 595

1
2 3 4 5
89