Tagged Questions
4
votes
4answers
5k views
How to totally fork a shell command that is using redirection
I've written quite a few shell scripts over the years (but I'm certainly not a sysadmin) and there's something that always caused me troubles: how can I fork a shell command immune to hangups in the ...
2
votes
2answers
2k views
grep output of expect script
I'm trying to run some expect language, and grep/parse the output all in one script. I want to grep the output of and look for "error" (I should note that standard linux commands like awk, sed, grep, ...
2
votes
1answer
42 views
diff two directories for changes and format output to use for script
I currently try to monitor two directories and subdirectories for changes in files. The directories contain the same set of files, some of them are changed. So I thought I might use the diff command ...
2
votes
1answer
131 views
What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts? [duplicate]
What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts?
For example:
filename=/etc/hosts
if [ -r "$filename" ] && [ -s "$filename" ]; then
md5sum $filename
...
2
votes
1answer
52 views
SSH Command behaves differently in Expect Script
I'm using this command on Server1
~# ssh root@Server2 /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
to append the contents of Server2's id_rsa.pub to the authorized_keys of Server1.
It ...
3
votes
3answers
2k views
Check whether files in a file list exist in a certain directory
The runtime arguments are as follows: $1 is the path to the file containing the list of files
$2 is the path to the directory containing the files
What I want to do is check that each file listed in ...
4
votes
5answers
1k views
Using $? in an if statement
function foo {
(cd $FOOBAR;
<some command>
if [$? -ne 0]
then
echo "Nope!"
else
echo "OK!"
fi
)
}
I am trying to write a function like the one above and place ...
0
votes
2answers
76 views
set variable options with dot
I'm trying to interface with a system not managed by me that is doing some Unix command scripts and command to easily execute a job.
There is a variable substitution that doesn't work and I wonder if ...
2
votes
4answers
229 views
Inserting a line in a file only if this line isn't yet part the file
Total newbie to shell scripting, I am searching to create a script to automatically apply some custom configuration to text based configuration files.
In the present case, I am searching to add 2 ...
2
votes
4answers
101 views
How to initialize an array using awk and bash?
I am trying to store values of the first line of a text file into an array. Here is what I have so far:
arr_values=()
awk '
NR==1 {
for (i=0; i<=NF; i++)
...
1
vote
1answer
32 views
Why isn't this XMLStarlet Query Working?
I'm trying to write a simple bash script that parses price info from the ebay developer API search results. Here's an example of XML search results for "Detective Comics 700":
...
3
votes
3answers
134 views
Howto run interactive commands, as another user
I'm trying to write a bash script that
context switches to another user (root, in this case)
executes a set of commands to i. create a user (interactively prompting invoker for username) then ii. ...
0
votes
2answers
25 views
Write log for a false statement of an if condition which returns exit 2
I have a script running every 5 mins with two exits in a condition clause.
#!/bin/bash
date=$(date +%Y)
if [ $date -eq '2014' ]
then
echo "Current year is $date"
exit 0
else
echo "Current ...
5
votes
4answers
7k views
Processing bash variable with sed
Been banging my head off a wall on this
bash variable LATLNG contains a latitude & longitude value in brackets like so
(53.3096,-6.28396)
I want to parse these into a variable called LAT and ...
1
vote
1answer
448 views
Script that unpacks a initrd, allows editing of the preseed.cfg and the packs it to cpio and gzip again
I want to program a script that allows what is said in the title. So basically I gunzip the initrd, than unpack the cpio, open vi to allow editing, save, pack with cpio, and gzip again, so nothing ...
3
votes
2answers
55 views
Error when calling mv with wildcards
#! /bin/bash
error_text=$(tail -n +1 /path/dir/folder/exc/a/update/Abd/ER/* | \
grep 'Warning\|Loaded\|Event')
echo $error_text
nc 10.10.99.45 25 << EOF
ehlo mail.abc.pvt
mail from: ...
3
votes
2answers
62 views
Checking the status of custom services with a script
I have a handful of shell scripts that start WebLogic managed servers. I need to design a master script that does the following:
Executes the shell script for a component\managed server
Checks to ...
0
votes
1answer
49 views
Is there a way of copying only directories and not files in bash? [closed]
I want to copy only directories from a folder on a remote server to my server. Is that possible?
Here is the code I have so far but it copies files and folders:
scp -rq ...
2
votes
4answers
1k views
Loop with 2 variables in a bash script
I am trying to utilize an API. I need to either do some type of "for loop" that replaces or utilizes 2 variables...
In pseudo..
# Declare New Servers
newserver=(
box001
box002
box003
box004
box005
...
0
votes
2answers
58 views
Simple Script to setenv tcsh or export bash
I am having problems with simple scripting, my Box is CentOS so bash is the default shell, now I wanted to send a line via setenv (tcsh) and would like to make a script that will ask me to input a ...
1
vote
2answers
83 views
Changing user to root when connected to a linux server and copying files
My script is coded in a way that doesn't allow you to connect to a server directly by root. This code basically copies files a server to my computer and it works but I don't have access to many files ...
2
votes
3answers
88 views
Need help to combine 3 split lines of bash script into one single line
The following is what I found after googling:
dpkg --list 'linux-image-*' \
| perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ ...
1
vote
3answers
205 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 ...
1
vote
1answer
44 views
Can't spawn backgrounded process
My team's using Pallet (think Puppet in Clojure) to automate server provisioning, but running into a fairly fundamental problem: Pallet can execute arbitrary scripts directly on target VMs, but if one ...
4
votes
1answer
38 views
Memory consumption of a script
Is there way to check the memory (physical & virtual) consumption of a script, of course in the moment of its execution.
Calculating the used memory minus the used memory before and after the ...
7
votes
2answers
292 views
Check bash/shell script dependencies
Is there a method/command to check for the dependencies of a bash script?
In other words, a response to this question :
Which libraries should a user install to run the script?
I can do this ...
4
votes
4answers
523 views
How to add a line to a file which has only root write permission and to continue the script execution
I am trying to learn bash scripting. I am working on a practical problem and at one point I need to add a line to a file which requires root permission to write.
The code looks like this:
# some ...
33
votes
8answers
41k views
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 ...
1
vote
1answer
132 views
Create a iptables rule if the rule does not all ready exist
Hi I've created a script to setup Nagios on my remote servers. My Script checks iptables to see if a firewall rule all ready exists and then is supposed to add the rule if it's not all ready there.
...
2
votes
1answer
23 views
Script. How to sort and move catalogs
I have some trouble writing a script. I don't know how to sort and move catalogs.
#!/bin/bash
while read -r line; do
mkdir "sd/$line"
done < 1a.txt
After reading information from 1a.txt ...
23
votes
6answers
5k 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?
0
votes
2answers
59 views
Different forms of executing a shell script
There are a few similar questions around that I've already seen, but I've come up with something different.
I'm reading this http://www.tldp.org/LDP/abs/html/invoking.html and I got confused when I ...
1
vote
4answers
706 views
How do I delete files with spaces in them in bash script?
this is a three part topic to a two part process that still is not working for me:
Objective: to manipulate a filename then delete it
the filename has spaces between the words then the dot .ext
...
5
votes
1answer
132 views
Save password in bash script for multiple queries
I would like to know if there is a safe way to only be queried for a password once in a custom bash script, and then save it somehow to be used for later needs. At the end of the script it should be ...
2
votes
2answers
33 views
Capture transcript of program with redirected input and output
A typical interaction for a program I've written might look like this:
Enter command: a_command
Completed a command
Enter command: another_command
Completed another command
I typically run my ...
3
votes
2answers
2k views
Determining if a host is online
I'm testing stability of a machine and I need way to simply write a Bash script to determine if a host is online or not. How could I script this?
if [ ! $(hostisonline) ]; then
# profit
fi
2
votes
1answer
46 views
Beginner question about bash “for loop”
When I do,
for ((i=0; i<"${ARRAY}"; i+=2))
do
echo $i
echo ${ARRAY[$i]}
done
echo $i works as I expected, also echo ${ARRAY[0]} works, but with $i as iterator i see only blank lines. How ...
4
votes
4answers
316 views
Bash Scripting : Printing column data in the same row
I have written a bash script to calculate the size of a PostgreSQL database and print the output along with the date when the script was executed in a text file. The script code is as follows:
...
3
votes
2answers
51 views
Bash script : help file inside the script or in a different file?
I'm writing a script that has the vocation to be a fully featured program at the end. As far as I know, BASH is enough for his purpose (manage PPA's, kinda like Y-PPA). I would like to know how to ...
1
vote
2answers
567 views
Bash shell scripting basic question regarding the syntax and basename
Consider the script below:
myname=`basename $0`;
for i in `ls -A`
do
if [ $i = $myname ]
then
echo "Sorry i won't rename myself"
else
newname=`echo $i |tr a-z A-Z`
mv $i $newname
fi
done
...
5
votes
1answer
187 views
When is it important to write portable scripts?
Most code I write is in PHP. I have recently started learning shell scripting. Most of the resources and tutorials that I've come across are specific to Bash. Some warn about bashisms and some don't. ...
2
votes
5answers
264 views
Delete all files in directories except those whose path are listed in a file
Given the following files:
data/A/a.txt
data/B/b.pdf
...
date/P/whatever.log
...
data/Z/z.jpg
I would like to delete all files in the data/A/, data/B/, ..., data/Z/ directories except those files ...
3
votes
7answers
699 views
Write a remote managing script
I need to be able to locally run a script that will connect to various servers and run commands on them. What is the best way to accomplish this?
3
votes
3answers
414 views
How can I extract/parse a complete URL from a semi random string?
I'd like to have bash parse/extract a full URL (and only the url) from a random short string.
Examples:
bob, the address is http://www.google.com
or
https://foo.com/category/example.html is up
...
20
votes
2answers
608 views
How do I move all files output from a command?
I have this grep command to find files without the word Attachments in them.
grep -L -- Attachments *
I want to move all the files that are output from that command. How do I do that in bash? Do I ...
6
votes
2answers
311 views
What's the most efficient way to grep for two completely separate things and assign the values to separate variables?
CentOS 6.x
I want to take the output from curl, grep for two completely separate strings, and assign their respective values as variables. What is the most efficient way to do this (without writing ...
0
votes
1answer
58 views
Prompting when redirecting content of script to remote machine
I have simple script:
#!/bin/bash
while getopts ":u:" opt; do
case $opt in
u)
USER="${OPTARG}"
;;
\?)
echo "Unknown flag has been used: -$OPTARG" >&2
exit 1
...
6
votes
1answer
4k views
How to automatically record all your terminal sessions with script utility
What I want to achieve is be able to record my terminal sessions to file automatically whenever I use Yakuake/Konsole.
It's easy to achieve if at the start of my session I do:
script -f ...
7
votes
1answer
206 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 ...
1
vote
2answers
54 views
Make Trickle get the correct $PATH
Trickle is a bandwidth limiting program. I have the following script for trickle.
#!/bin/bash
echo $PATH
trickle -s -t 3 -u 200 -d 200 "$@"
I have a script in ~/bin/ that want to run the script ...