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
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
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
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
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
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
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
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
44 votes
14 answers
30k views

Using notify-send with cron

I am using Arch Linux with KDE/Awesome WM. I am trying to get notify-send to work with cron. I have tried setting DISPLAY/XAUTHORITY variables, and running notify-send with "sudo -u", all without ...
justsomeone's user avatar
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
31 votes
4 answers
142k 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 ...
Vass's user avatar
  • 5,271
11 votes
4 answers
43k views

How to use wc and piping to find how many files and directories are in a certain directory?

How can I use word counter (wc) and piping to count how many files or directories are in the /usr/bin directory?
cash's user avatar
  • 173
7 votes
4 answers
12k views

Print two files in two columns side-by-side

I want to output two text files in two columns — one on the left side and other one on the right. paste doesn't solve the problem, because it only insert a character as delimiter, so if the first file ...
belkka's user avatar
  • 481
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
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
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
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
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
12 votes
2 answers
6k views

Piping for loop output prevents local variable modification

I'm trying to write a simple bash function that takes, as its arguments, a number of files and/or directories. It should: Fully qualify the filenames. Sort them. Remove duplicates. Print all that ...
tjm's user avatar
  • 223
11 votes
1 answer
11k views

How does this find command using "find ... -exec sh -c '...' sh {} +" work?

@StephaneChazelas posted the following solution to this Q&A: Having some trouble using “find -exec {} +”. $ find . -iname "*.extension" -exec sh -c ' exec <command> "$@" <additional ...
slm's user avatar
  • 364k
4 votes
6 answers
38k views

Check for Process if Same is Running

Is there a script or a way in linux that when I try to execute a shell script/process, if the same is running, it will prompt that same is running and will exit otherwise it will continue.
user60216's user avatar
  • 325
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
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
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
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
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
28 votes
1 answer
9k views

How to implement a horizontal cat? [duplicate]

Standard cat concatenates files line by line (row by row, if you will). I find myself needing a horizontal cat command more and more often recently; i.e. a command that takes a list of files and ...
Konrad Rudolph's user avatar
23 votes
8 answers
43k views

History command inside bash script

History is a shell-built in command I couldn't able to use that within a BASH script. So, Is there a way attain this using BASH script ? Here we go my script for you: #!/bin/bash history | tail -100 ...
Vijay Ramachandran's user avatar
10 votes
4 answers
31k views

Print a string including single quotes and other special characters

How can I write this with an AIX script correctly? My requirement is to write this command in test.txt: clock=$(prtconf -s | awk '{print $4,$5}') I tried this command: print 'clock=$(prtconf -s | ...
batil's user avatar
  • 311
6 votes
2 answers
2k views

How does bash interpret the equal operator with no surrounding spaces in a conditional?

The following script does not behave as I would have expected. Adding spaces around the '=' in the conditional made it perform how I wanted, but it got me thinking, what is it actually doing inside ...
Cory Klein's user avatar
  • 18.4k
4 votes
3 answers
18k views

Run a GUI program at startup

I am using Ubuntu 14.04. I wrote a small script named trial. The contents of the script are as follows: #!/bin/sh SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin sh -c firefox I copied the ...
Joker's user avatar
  • 161
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
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
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
24 votes
3 answers
80k views

Execute a command on user logon

In Linux desktop system, I want to execute a command when the user logs in. After reading some other posts, I tried to insert the command in ~/.bashrc but unsuccessfully. Moreover, the system uses a ...
BowPark's user avatar
  • 4,821
18 votes
8 answers
37k views

Add something to crontab programmatically (over ssh)

I have a deployment script.  It must add something to a user crontab (trigger a script that cleans logs every XXX days); however, this must be done only during the first deployment, or when it needs ...
sliders_alpha's user avatar
7 votes
1 answer
3k views

Parallel execution of bash script

I have a table URL_Experiment in my database (mySQL database). I have 2 million URL links in this table. For each URL, I am checking if some particular text is present in the URL and updating the ...
Ramesh's user avatar
  • 38.7k
3 votes
1 answer
2k views

Integration of afuse user-level automounter with sshfs as a systemd service spawned from login for local users

Edit 2020: I've been using the systemd-answer posted below for several years now, and am quite happy with it. To facilitate working with remote files, I setup afuse with sshfs to run from .bashrc of ...
Alex Stragies's user avatar
1 vote
1 answer
969 views

$! not being set to the PID of a > >(...) process substitution used with an external command

Bash 4.4.19(1)-release I have below a simple script which is the basis for a logging app. For various reasons I had to use process substitution. The runner is the heart of the app and since ...
conanDrum's user avatar
  • 457
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
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
19 votes
10 answers
40k views

Extracting nested zip files

I have numerous zip archives, each of which contains a number of zip archives. What is the best way to recursively extract all files contained within this zip archive and its child zip archives, that ...
oadams's user avatar
  • 2,305
18 votes
3 answers
62k views

Bash: calculate the time elapsed between two timestamps

I have written a script that notifies me when a value is not within a given range. All values "out of range" are logged in a set of per day files. Every line is timestamped in a proprietary reverse ...
Marco's user avatar
  • 408
15 votes
3 answers
7k views

Can I use a variable in a Bash brace expansion?

Below is some sort of pseudo-code for what I'm trying to accomplish: #!/bin/bash # I already have the variable below figured out (positive integer): numlines=$([returns number of lines containing ...
rubynorails's user avatar
  • 2,243
12 votes
5 answers
21k views

How do you colorize only some keywords for a bash script?

I am running some unit test code. The unit test code outputs regular text. There is a lot of the text so I want to highlight for the user important keywords. In this case the keywords are "PASS" and "...
Trevor Boyd Smith's user avatar
7 votes
5 answers
17k views

Bash script: check if a file is a text file [closed]

I am writing a menu based bash script, one of the menu options is to send an email with a text file attachment. I am having trouble with checking if my file is a text file. Here is what I have: ...
Powea's user avatar
  • 73
4 votes
4 answers
6k views

passing passwords to a script

I want to scp a file to a remote host, and from there, run a command on the file, and exit the remote session. If I write a usual script, call scp and then ssh, I am prompted to enter two passwords. ...
eli's user avatar
  • 303
4 votes
2 answers
2k views

Commands for determining level of usage of server

I am scripting a new program that will connect to a cluster of machines in a VM park, and hopefully check their level of usage. The VM clients are a combination of RHEL and CentOS, and runs on ...
larhauga's user avatar
0 votes
1 answer
2k views

/bin/sh: ./check-dependencies.pl: not found — but check-dependencies.pl exists!

I'm trying to build Moses, but when I run make I get the following output: minakshi@minakshi-Vostro-3500:~/Desktop/working-dir/moses/scripts$ make release # Compile the parts make all make[1]: ...
jenn's user avatar
  • 21
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
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

1
2 3 4 5
11