Questions tagged [shell-scripting]
Programming in an Interpreted language executed by a running Shell
622
questions
182
votes
8
answers
415k
views
How do I sleep for a millisecond in bash or ksh
sleep is a very popular command and we can start sleep from 1 second:
# wait one second please
sleep 1
but what the alternative if I need to wait only 0.1 second or between 0.1 to 1 second ?
...
48
votes
6
answers
53k
views
How do you set a locale non-interactively on Debian/Ubuntu?
Usually, I run aptitude -y install locales then dpkg-reconfigure locales to set up locale.
Now I want to put it into a shell script, how can I reliably do the following, automatically / non-...
30
votes
2
answers
43k
views
Is it possible to set a timeout on openssl's s_client command?
I've got a script which uses openssl's s_client command to pull certificates for a big set of hosts. Some of these hosts will inevitably be unreachable because of a firewall. Is it possible to set ...
27
votes
9
answers
56k
views
Running ssh-agent from a shell script
I'm trying to create a shell script that, among other things, starts up ssh-agent and adds a private key to the agent. Example:
#!/bin/bash
# ...
ssh-agent $SHELL
ssh-add /path/to/key
# ...
The ...
22
votes
2
answers
26k
views
How to grant sudo rights only to specific script files?
I would like a user to have sudo rights (without password check) to a couple of shell scripts under a specific directory (in my case, /usr/local/tomcat7/bin), and to nowhere else. What's the simplest ...
22
votes
1
answer
11k
views
Ctrl-C in bash scripts
How do I implement ctrl+c handling in bash scripts so that the script is interrupted, as well as the currently running command launched by the script?
(Imagine there's a script that executes some ...
20
votes
7
answers
34k
views
Get list of transferred files from rsync?
I'm currently using rsync on a script that deploys a PHP application from a staging to a production server. Here is how:
rsync -rzai --progress --stats --ignore-times --checksum /tmp/app_export/ root@...
20
votes
1
answer
5k
views
Shell command slow when using pipe, fast with intermediate file
Does anyone understand this huge difference in processing time, when using an intermediate file, or when using a pipe? I'm converting tiff to pdf using standard tools on a fresh debian squeeze server. ...
18
votes
2
answers
7k
views
What does a minus sign inside of dollar brackets of a shell script mean?
In an existing shell script, I'm seeing some variables referenced that either include or end with a minus sign. For example:
PID=${PID-/run/unicorn.pid}
and:
run_by_init() {
([ "${previous-}" ]...
17
votes
3
answers
25k
views
Does mysqldump return a status?
I am creating a script that backups a mysql db using the mysqldump utility. I am writing this script in the shell "sh". I would like to capture the output status of mysqldump in the script (i.e. if ...
16
votes
4
answers
13k
views
How to use find command to delete files matching a pattern?
I'm trying to write a bash command that will delete all files matching a specific pattern - in this case, it's all of the old vmware log files that have built up.
I've tried this command:
find . -...
14
votes
2
answers
37k
views
How to set postgresql user password in bash script
I want to set a password for the default Postgresql server user, postgres. I did it by using:
sudo -u postgres psql
# \password postgres
I want to do this step in many machines, so I would like to ...
14
votes
6
answers
39k
views
Connect to MySQL through command line without using root password?
I'm building a Bash script for some tasks. One of those tasks is create a MySQL DB from within the same bash script. What I'm doing right now is creating two vars: one for store user name and the ...
14
votes
3
answers
9k
views
Automated graceful reload of gunicorn in production
I have an automated deployment workflow that pushes code out to my production servers and triggers database migrations, static file updates, etc. Problem is, gunicorn doesn't automatically reload code ...
14
votes
3
answers
31k
views
How to pass command output as several arguments to another command
I have a command that produce a output like this:
$./command1
word1 word2 word3
I want to pass this three words as arguments to another command like this:
$ command2 word1 word2 word3
How to pass ...
12
votes
5
answers
6k
views
Can a shell script wait for a file to change and take action?
I wonder if I can write a script that will monitor for a change in a file and execute some action when the change is detected.
Detailed explanation:
OpenVPN writes its status to a file every 1 ...
11
votes
5
answers
18k
views
Silent and scripted install of CPAN and Perl modules?
I need to install CPAN and some Perl modules automatically in a Scientific Linux (RHEL) installation script. Unfortunately the specific modules I want (at least one of them) cannot be found as RPM:s ...
11
votes
3
answers
31k
views
Powershell Parameters
I have a Param block in my script
Param (
[Parameter(Mandatory=$True)]
[string]$FileLocation,
[Parameter(Mandatory=$True)]
[string]$password = Read-Host "Type the password you would ...
11
votes
2
answers
14k
views
Can I change the ownership of all the files of a specific user?
Is there a way to recursively find all files owned by a user and change them to another user/group in Gnu/Linux?
I assume there must be some magic one liner but my command line wizardry skills are ...
11
votes
4
answers
34k
views
How can I use openssl to get results from HTTP GET requests?
I need to use openssl to perform some HTTP GET requests in a shell script. The line I'm using to do this right now is shown below. This is parsing the content of an XML response of the following ...
9
votes
3
answers
8k
views
Echo each shell script command with a timestamp
set -x or set -v prints every executed command.
How do I get the command printed with the time when the command started executing?
9
votes
4
answers
39k
views
control a bash script with variables from an external file
I would like to control a bash script like this:
#!/bin/sh
USER1=_parsefromfile_
HOST1=_parsefromfile_
PW1=_parsefromfile_
USER2=_parsefromfile_
HOST2=_parsefromfile_
PW2=_parsefromfile_
imapsync \
-...
8
votes
5
answers
4k
views
Run a script from anywhere
I have a script:
#!/bin/bash
echo "$(dirname $(readlink -e $1))/$(basename $1)"
that sits here: /home/myuser/bin/abspath.sh which has execute permissions.
If I run echo $PATH I get the following: /...
8
votes
3
answers
58k
views
How to run a script automatically on system startup in centos
I've successfully installed TeamCity ib CentOS 6.2 Minimal. I'm stuck on trying to run a script automatically on system startup:
/opt/TeamCity/bin/teamcity-server.sh start
I've googled around and ...
8
votes
3
answers
15k
views
From a shell script, how can I check whether a table in MySQL database exists or not?
I am trying to write a script which allows a user to select the what manipulation he needs to do on a table. I want to check if the table exists or not. If it exists I will continue the other things ...
8
votes
4
answers
3k
views
Loop through servers and run command
I have a set of servers mentioned in a text file called network_list.txt . How can i run through the servers and run command and display the result ? I have tried the follows :
filename="network_list....
8
votes
3
answers
18k
views
"watch" command to notify on newly created files on linux
watch command to notify on newly created files on linux
How to modify the below command to notify on creating a new file to /usr/local/mydir/ by linux user john ?
watch -d 'ls -l /usr/local/mydir/ | ...
8
votes
3
answers
5k
views
How to delete docker images older than x days from docker hub using a shell script
How to delete docker images from docker hub using a shell script.
I want to delete all images from docker hub older than past 50 days of a private docker hub account.
Any ideas? Which tools to use ...
8
votes
1
answer
3k
views
using temporary files vs pipes advantages and disadvantages
Say I have a file named jobs.csv and I would like to get the top 50k jobs done by Foo
I can either do:
# cat jobs.csv | sort -u | head -n 50000 > /tmp/jobs.csv
# cat /tmp/jobs.csv | while read ...
7
votes
2
answers
14k
views
expect script + expect miss the send string + delay issue
I write the active.ksh script (based on expect) in order to login automatically to some Solaris machine and execute the hostname command (login to virtual IP in order to verify which hostname is the ...
7
votes
2
answers
26k
views
Shell script for docker ps -a | grep to find number of certain containers running
I want to write a script that is executed by my development build server that will remove any 'similar' docker containers before building and running a new container.
Below is pseudo code for the ...
6
votes
4
answers
3k
views
Robust way to resolve a DNS address in a script (IPv4 (A) and IPv6 (AAAA))?
I have a domain name in a bash variable ($TARGET), and I want to get the IPv4 (A record) address of it in my bash script (I also want to get the IPv6 AAAA record (if exists)), in a robust manner.
i.e. ...
6
votes
3
answers
7k
views
Simple dig output?
In a script I want to be able to write an IP address to somewhere easily, so I thought using dig (or a similar command) with back-ticks.
However the simplest output I've been able to come up to wrt ...
6
votes
2
answers
18k
views
Linux FTP upload: "No such file or directory", but file exists
I want to upload backup archives from one server to another server using ftp. In my backup cronjob I use this script to upload files:
MEDIAFILE=/var/somedir/somefile.encrypted
if [ -r $MEDIAFILE ]
# ...
6
votes
4
answers
17k
views
Incrond running but not executing commands CentOS 6.4
I have copied this question over here from StackOverflow...
I have installed incron from the EPEL repository (0.5.9) (before you ask; YES, I also tried downloading the source and compiling locally (0....
6
votes
2
answers
5k
views
linux + how to identify if file is pointed by link/s
I create new file called - 192.9.200.1
touch 192.9.200.1
then I create new link that will be pointed to 192.9.200.1 file
ln -s 192.9.200.1 file
so finally I get:
ls -ltr /tmp
-...
6
votes
1
answer
1k
views
perl equivalent of sh -e
In /bin/sh and /bin/bash (and I guess a lot of other shells), starting scripts with #!/bin/sh -e (or executing set -e in someplace in the script) would cause the script to abort when any command line ...
6
votes
3
answers
399
views
Returning A List Of Quoted Files Paths Using Find
I would like to create a tarball that contains files from a directory that have a ctime of less than 30 days. Most tutorials I have found have basically recommended doing something like this:
tar cvf ...
6
votes
1
answer
2k
views
Is it possible to listen to a TCP port only with a shell, with no additional tools? [closed]
I need a very simple web server on a very small embedded system with a MISP processor. I thought that the simplest server could be a shell script listening to a TCP port.
The problem is that the ...
6
votes
1
answer
439
views
identifying ssh trusts between multiple servers on a network
The problem:
We have many dev/qa/prod RH/Solaris servers with many accounts having ssh trust between them, including between servers in different environments (prod->prod, but also qa->prod). I know ...
5
votes
2
answers
4k
views
FreeBSD rc.d script doesn't start as a daemon
I have developed the following script at location /usr/local/etc/rc.d/bluesky
#!/bin/sh
# PROVIDE: bluesky
# REQUIRE: mysql sshd
# BEFORE:
# KEYWORD:
. /etc/rc.subr
name="bluesky"
rcvar=...
5
votes
2
answers
15k
views
ssh fails to execute remote command when run from cron bash script - works from CLI
I have a script, written in bash, which runs from cron. One of the first things it does is SSH to a remote host and retrieve a list of files in a directory. Everything works fine when run from the ...
5
votes
1
answer
12k
views
How can I replace line #6 in a text file in a bash script?
I have a text file that is generated by 3rd-party software as part of a build process. I want to replace one specific line - in my case, line 6. How can I do this in my bash script?
I suspect sed is ...
5
votes
1
answer
4k
views
Closing All Shared Files on the Network [duplicate]
I am trying to close all of the open files in the shared folders of a server via PowerShell script. I found the following script which only close files on one drive (F:), however, this server has 3 ...
5
votes
2
answers
5k
views
Installing and configuring phpmyadmin completely through a shell script
Referencing this tutorial: https://www.liquidweb.com/kb/how-to-install-and-configure-phpmyadmin-on-ubuntu-14-04/ for installing PHP-Admin, after installing the package, on "Step 2: Basic Configuration"...
5
votes
3
answers
2k
views
Bash-Scripting - Munin Plugin don't work
i have written a munin-plugin to count the http-statuscodes of lighttpd. The script:
#!/bin/bash
######################################
# Munin-Script: Lighttpd-Statuscodes #
########################...
5
votes
2
answers
16k
views
How properly handle deletion of pid.file in service script
I'm trying to write service script for application. So I can control it like this:
./myscript.sh start|stop|status
On startup pid.file with process id creates, and based on it I can check status and ...
5
votes
2
answers
4k
views
expect + how to identify if expect break because time out?
The target of the following simple expect script is to get the hostname name on the remote machine
Sometimes expect script fail to perform ssh to $IP_ADDRESS ( because remote machine not active , etc ...
4
votes
2
answers
7k
views
Unix separate multiple commands which has '&' (execute in background) in the end
To separate regular commands in Unix is to put semicolon in the end like this:
cd /path/to/file;./someExecutable;
But it seems not working for commands like this:
./myProgram1 > /dev/null &
....
4
votes
3
answers
6k
views
Access windows file system from linux shell script
I want to know, whether is it possible to access the Windows files from a shell script on Linux system ?
What i am trying to do is to run a shell script in my Linux system, that will access the files ...