Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

learn more… | top users | synonyms (2)

2
votes
3answers
5k views

solaris 10 + display 2 lines after match by grep?

I want to match for example: The string e1000g0 from the lltconfig -a list , and then to display the two lines after the string e1000g0 so I get only the lines: Node 0 du1a : ...
0
votes
1answer
229 views

How to effectively use bash scripting (different cost reductive ways and good habits)? [closed]

This is one of the most perennially important questions. How to effectively script ? What are the bad ways and what are the good ways ? I know of some, which i will point out here. If anybody does ...
2
votes
1answer
3k views

Executing a script from Nagios event handler fails to run

I have Nagios running on a webserver. For this one Nagios service check in particular, if it fails, it will run a script. This script is triggered via Nagios event handlers. Nagios event handler ...
11
votes
4answers
4k views

Executing user defined function in a find -exec call

I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1). The following code doesn't yield any result: function foo { echo "Hello World" } find somedir -exec ...
5
votes
2answers
1k views

Requesting user input while reading file line by line

For class I need to write a Bash script that will take the output from ispell and when I try and request user input inside the while loop it just saves the next line of the file as the user input. ...
4
votes
2answers
2k views

Is there an easy way to create a FreeBSD rc script?

I have a FreeBSD jail in which I run a server using the command: /usr/sbin/daemon /path/to/script.py At the moment I have to run this command every time I restart the machine and the jail starts. ...
1
vote
1answer
493 views

How to create script that toggles one value in synclient?

I use synclient touchpadoff=1 to disable touchpad and synclient touchpadoff=0 to enable touchpad. $synclient | grep TouchpadOff TouchpadOff = 1 I'd like to create script that will ...
1
vote
2answers
383 views

List info about files/directories with spaces

I am trying to use ls to get information about files and directories. My current command fails to properly get the name whenever a file/directory has a space. I am using this to list all ...
3
votes
1answer
6k views

show the year while listing files in the current directory

I am working on a red hat server I want to list the files in a way where the year when each was file created would appear in the Date how is that possible ?
7
votes
3answers
664 views

shell scripts are still working without #!(sha-bang line)

I am new to shell scripts and many books have written that use #!(sha-bang) line in starting of the script to invoke the interpreter .And this will invoke a new shell for script and do the ...
3
votes
1answer
4k views

Identify the number of unique values and then the number of occurrences of those values in the file

I have a data file which contains 15000 lines, but only 400 unique values. I am looking for a way to identify the number of unique values and then the number of occurrences of those values in the ...
2
votes
1answer
4k views

How to debug csh scripts?

My lab uses csh scripts to run jobs. It is usually difficult for me to debug a shell script, so I'm wondering if there is a csh debugger I can use. I know there are some flags like -x or -v that can ...
0
votes
2answers
10k views

How to change the working directory of invoking shell using a script? [duplicate]

Possible Duplicate: changing current working dir with a script I am trying to create few scripts that would change the working directory of the main shell/terminal. Not able to do so. I ...
5
votes
3answers
10k views

Find files which are created a certain time after or before a particular file was created

I need a shell script which finds files which are created 1 hour before or 1 hour after a particular file (test.txt) was created. If I go with find -newer, that means I'd have to create a temporary ...
1
vote
1answer
895 views

How to manage getops argument assignation

This is my first question. I'm trying to make a small script with options and I'm using getopts to do it. The weird thing is: The argument don't get assigned to the variable or it always enter the ...
0
votes
1answer
1k views

seq - invalid floating point argument error

I have a file with many numbers, with each number on one line. My goal is to find the numbers that are missing. I'm trying to generate the sequence of all the numbers with seq start=$(head -1 ...
2
votes
2answers
1k views

BASH: Grouping files by name

I have over one million files. And I have to proceed over them. My files' directory hierarchies just like below source=/opt/output/renamed/ target=/opt/output/combine send=/opt/output/send/combined ...
3
votes
6answers
211 views

Extracting help message from script itself

I want that my help message be extracted from the script itself. #!/bin/bash # # foo - do things # Author: John Doe <jhon@doe> # ---------------------------------------------- # SYNOPSIS # ...
4
votes
2answers
952 views

Get line number in a Bourne shell script

I'd like to be able to print the current line number in a shell script. I know about the $LINENO variable in Bash shells, but it doesn't seem to exist in Bourne shells. Is there any other variable ...
0
votes
1answer
686 views

Shell script to recursively grep data from certain files in the directory and return values to file [closed]

I am making a simple shell script which will minimize the time I spend in searching all directories under a parent directory and grep some things inside some files. Here's my script. #!/bin/sh ...
3
votes
2answers
735 views

How do I set a script that it will run on start up in FreeBSD?

I am using FreeBSD 9 and I want to make a script that will check if an Ethernet cable is connected, and if so, connects to it. I want to set it so that it runs whenever the machine starts up, or ...
2
votes
1answer
1k views

Inserting a file into another file using sed

I want to insert the contents of file1 into file2 after a matching PATTERN. I want to do it only after the first occurrence of the PATTERN. I would like to know the modification I need to make to ...
2
votes
1answer
407 views

Script to Read Videos and output information

I am working on a script that reads all folders inside a Movie folder and outputs the information for each video in a file. The current one I have is this: movies=0 rm movielist for dir in Movies/* ...
0
votes
2answers
650 views

batch rename a few files [duplicate]

Possible Duplicate: Batch renaming files I have some files that I wish to rename in a single command. The files are names thus. I want the E S Posthumus bit removed from the names and also ...
1
vote
1answer
344 views

What is the canonical way to implement order independent options in bash scripts? [duplicate]

Possible Duplicate: How do I handle switches in a shell script? Most common shell commands allow the user to specify options in any random order. Positional Parameters like $1 as commonly ...
16
votes
2answers
12k views

Execute a command before shutdown

I want to execute a simple command just before the computer shuts down (timing is not essential). For startup I can use /etc/rc.local - is there something similar for shutdown? Note that I would ...
3
votes
3answers
23k views

linux + add X days to date and get new virtual date

I have Linux ( RH 5.3) machine I need to add/calculate 10 days plus date so then I will get new date (expiration date)) for example # date Sun Sep 11 07:59:16 IST 2012 So I need to get ...
17
votes
2answers
4k views

How can I test for POSIX compliance for shell scripts?

Probably the main thing that this website has taught me so far is the importance of writing portable shell scripts. Considering that POSIX is the closest thing to a common standard between all ...
4
votes
1answer
2k views

How to find out common elements between two files?

For an example, I have 2 files having following info: File #1: 12 13 14 15 File #2: 12 1 13 2 14 2 15 6 16 7 17 8 Output File: 1 2 2 6 In the output file, I want only the second column ...
0
votes
0answers
38 views

How do I decide whether to make a command a shell script or an alias? [duplicate]

Possible Duplicate: In Bash, when to alias, when to script, and when to write a function? If I want to make a command like gsb that will expand to something like git show-branch -a | more ...
2
votes
3answers
608 views

Find word in sentence with Bourne Shell (sh)

I'm trying to determine if a particular word appears in a sentence using Bourne Shell (sh). For example: #!/bin/bash INPUT='Lorem ipsum dolor sit amet.' if [[ "$INPUT" == *dolor* ]] then echo ...
5
votes
3answers
1k views

How to poll existence of a background script correctly

I have a problem with the following kind of script: #!/bin/sh long_running_script.sh & while [ `pidof long_running_script.sh` ] do echo "." sleep 1 done The sript will ...
2
votes
1answer
663 views

How to get the PID from a spawned process in expect?

Example: In a shell script I open a SSH tunnel whit this: /usr/bin/expect <<EOD set timeout -1 spawn ssh -fNL localhost:1873:localhost:873 HOST expect "*?assword:" { send "$SSHPASS\r"} sleep 2 ...
1
vote
2answers
1k views

Pattern matching exception for shell script

So I have the following shell script for batch renaming the episodes of some given TV series: #!/bin/bash #script for renaming files of form "*01*.ext" to "Episode 01.ext"; counter=0 for all_files in ...
2
votes
5answers
10k views

Bash iterate over a list of strings

I have this bash script: for opt in string1 string2 string3 ... string99 do somestuff It works, but I would like to replace the explicit listing of my strings with a file which actually contains ...
4
votes
2answers
158 views

Dealing with script interruption

I need to iterate over between 120k and 500k files. find handles this well. find $PWD -type f -path "fragments/*.pdbqt" For some reason I want to list the same set of files again, in the same ...
8
votes
4answers
3k views

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func ...
1
vote
2answers
157 views

run bash in script

I want to creat a script to run an another script with a several parameter exp=([1]=bloc [2]=ins [3]=rep [4]=op) for j in ${!exp[*]} do arr=([1]=mem [2]=gen [3]=usr) for i in ${!arr[*]} ...
4
votes
1answer
689 views

How is this sed command working?

I want to understand, how following sed command is working: sed -nre "/jar$/ { x; d; }; /class$/ { x; p; } Actually it is part of another command to find a particular class file from the given list ...
2
votes
1answer
546 views

Predict filename before downloading from a URL, in shell script

I have a shell script that downloads files from a list using wget, and resumes automatically if there is any non-critical error (because of unstable WIFI during storms). The problem is, I want to ...
4
votes
1answer
166 views

Output redirection and spaces

I'm paranoid about this, but hopefully it's a simple question: Is there any difference between cat file1 | egrep -oP "[Mm]y string" > /home/user/file.txt and cat file1|egrep -oP "[Mm]y ...
2
votes
2answers
2k views

Sharing Variables across sub shell scripts

I have a Main shell that runs a profile, The profile contains some variables such as Count. When I call the main shell, it loads the profile and then call another shell script. I want that inner shell ...
2
votes
1answer
203 views

A short way to call my bash script instead of use Source command

My script use and modifies a shell variable. I need to always use: source myscript Is there a shorter way to call this script and have it modify the current shell's environment? It can be ...
1
vote
0answers
36 views

Redirect hard return to other terminal [duplicate]

Possible Duplicate: Construct a command by putting a string into a tty Suppose I've run a command in a terminal ( /dev/pts/1 ) and it starts to ask me for input ( "press y to confirm" ) and ...
1
vote
1answer
712 views

Regular expression how to get value in shell script?

I have to write a shell script to get value from a string, something like: string="abcdef" pathName How can I use grep command with the use of regular expression to get "pathName"?
4
votes
2answers
7k views

Recovered deleted files on Mac by command line

I accidentally deleted a file by running: rm -rf ./Desktop/myScript.sh I think it's possible to recover the file, because the rm command removes temporarily. How can I recover the deleted file on ...
1
vote
1answer
98 views

Redirection doesn't show up as an argument to a sourced script

My problem is that I can't print to the screen when I write a bash script . myBashScript.sh < text.txt In myBashScript.sh I just print out echo $2 But instead of printing "text.txt", it ...
2
votes
2answers
315 views

Create a new link to access all files and folders

I get stuck to create a shell script for a new link something like that link linkPathName orginalPathName I want to create a link between a new link and original path; however, both can access ...
20
votes
3answers
10k 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 ...
1
vote
3answers
4k views

Logical AND in a bash script

I have an if in my bash script that has to check if EITHER of the 2 files exists, if they don't exist it should echo match. Code I have: if [[ ! -f /etc/benchmarking/code ]] && [[ ! -f ...