Tagged Questions

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

learn more… | top users | synonyms (2)

2
votes
3answers
590 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
642 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
9k 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
156 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 ...
7
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
681 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
534 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
157 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
1k 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
196 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
706 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
288 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 ...
19
votes
3answers
9k 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 ...
2
votes
2answers
375 views

Am I using bash after this ordeal?

On Mountain Lion, host Coguaro, I login as user coconutpowder using bash: Coguaro:~ coconutpowder$ echo $0 -bash I need to run a command with elevated privileges so I try to su switch to root but ...
3
votes
1answer
527 views

Implementing a String -> PDF Stamp tool

I wrote a little shell function which creates a fixed-size PDF file (A4) from a character string using gs: make_stamp() { file=$1 string=$2 tmp=${file}-small.pdf gs -o "$tmp" -sDEVICE=pdfwrite ...
5
votes
4answers
1k views

GUI input for shell script

I have a shell script which installs a program in RedHat. I need to add a GUI for entering some parameters so that it looks like an installation wizard . I tried Xdialog, Zenity, and Yad, but they ...
2
votes
1answer
425 views

Command substitution: file or command not found

The following script #!/bin/bash QUERY='select * from cdr;' MYROWS=$("sqlite3 -list -nullvalue NULL -separator ',' /var/log/asterisk/master.db '${QUERY}'") gives me ./bla.sh: row 35: sqlite3 -list ...
1
vote
1answer
493 views

Parsing SQLite output

Using -line Option I get this output from SQLite: row1_column1 = valueA row1_column2 = valueB row2_column1 = valueC row2_column2 = valueD So one line for each column value and result rows are ...
3
votes
2answers
10k views

In a bash script, using the conditional “or” in an “if” statement

This question is a sequel of sorts to my earlier question. The users on this site kindly helped me determine how to write a bash for loop that iterates over string values. For example, suppose that ...
5
votes
1answer
7k views

In a bash shell script, writing a for loop that iterates over string values

In bash, I know that it is possible to write a for loop in which some loop control variable i iterates over specified integers. For example, I can write a bash shell script that prints the integers ...
2
votes
2answers
122 views

Is there any way I can fit this into my ~/.bashrc as a function?

I just discovered this useful bit of code on this useful-looking website. #!/bin/sh exec tclsh "$0" ${1+"$@"} proc main {} { set lines [lrange [split [read stdin] \n] 0 end-1] set count ...
1
vote
2answers
638 views

What scripting language or platform to use for web page downloads and screen interaction? [duplicate]

Possible Duplicate: Does anybody here have experience in automating some tasks in web applications using curl? Here is what I need to do? Wondering what platform is most suited - easy to ...
2
votes
3answers
1k views

Timeout `idql` command in ksh

I probably read all the threads in almost all the forums for a solution to my need. I am a beginner in shell scripting and I dont have a perfect solution yet. Below is my code snippet. idql -n ...
0
votes
1answer
778 views

I am trying to parse data on two delimiters

So what I'm trying to do is for this: looking at the example CSV: 1,3917,3917,BGP=694|Ethernet=1610|LAG=3,Y After the script is run, the output should look similar to: 1,3917,3917,BGP=694,Y ...
0
votes
1answer
3k views

Script failing with “command not found: ^M”

When I try to run the following script in zsh, via the command /bin/zsh ~/.set_color_scheme.sh I get the following error: command not found: ^M The script has u+x permissions and it used to work on ...
3
votes
1answer
421 views

Extract directory from wget's stdout

I am trying to wget a tarball from github.com and, without creating a temporary file, extract a subdirectory from it: wget -qO- https://github.com/django-nonrel/django-nonrel/tarball/develop | tar ...
1
vote
1answer
78 views

Social website about shell commands

I remember seeing an entire social website devoted to the question What are your favorite command line features or tricks?, where you can post shell commands, tag them, comment on them, and rate ...
3
votes
1answer
388 views

Shell script to execute a command with iteration

This is a script which looks for .tab files in a folder an should imports them with ogr2ogr, but I fail with the correct bracing. for i in $( ls *.tab ) do echo item: $i ogr2ogr -f PostgreSQL ...
2
votes
3answers
398 views

Return value of commands when using stdin?

Imaging I am executing the following command: echo "root:mypass" | chpasswd How can I get the return code / error code returned by chpasswd? Is that possible in this case at all? I tried RET = ...
2
votes
2answers
1k views

How to make duplicates with different names from a single file? [closed]

I want to duplicate the contents of a file. Suppose there is a file named "Hydrogen.element". I want to duplicate the contents of this file with a different name ie.make another file named ...
1
vote
3answers
2k views

How to rename files using sed?

This is an sed expression that was created by a contributor here. More specifically by @craig-sanders. $FileToMove is set to "2012-09-01 Home insurance quote_zz20.pdf" # use sed to extract folder ...
3
votes
1answer
2k views

How to move file based on file name expression using shell script?

I want to achieve the following using a shell script. When a script is called with a file as a parameter, it should move the file based on a portion of its name. For example, "Car insurance ...
7
votes
3answers
1k views

Script to toggle setxkbmap

I would like a bash script that toggles between: setxkbmap se and setxkbmap us. Then my intentions are to map that script to CtrlEsc through the Keyboard > custom shortcuts. The point is that I ...
5
votes
1answer
5k views

Executing a script in zsh - file permissions

I'm confused about execute file permissions not behaving as I expect. Probably because my expectations are wrong. Anyway: I have a script file, for simplicity is just called s, located in ~/bin. For ...
1
vote
2answers
625 views

Remove all packages in a text file via apt-get

I have a text file with a package on each line. How can I remove all the packages in that text file from the system? Also, if one package cannot be removes/wasn't installed in the first place can I ...
1
vote
1answer
2k views

How to create a md5-file for every folder in a drive recursivley?

I'm searching for a script which creates a md5 chechsum file for every single folder inside drive recursivley? I do have a copy of md5deep on my machine but I'm not that good in scripting bash.
0
votes
2answers
2k views

Deleting many files except one type [duplicate]

Possible Duplicate: Deleting all files in a folder except files X, Y, and Z I am writing a script to compile many .tex files. After that, many auxiliaries files were created. So, I would ...
3
votes
2answers
2k views

exec < filename - what does this do?

I have found the following code snippet (sh): TESTFILE=$TEST_PATH/test.out if [ -f $TESTFILE ] then exec < $TESTFILE else echo "$TEST_PATH: no test.out file" fi ...
3
votes
3answers
512 views

How to remove a trailing CRLF from a file?

I have a binary file with a trailing CRLF (2 bytes total) that I want to remove. How should I do it? Please note that I'm using busybox and I don't have perl.
5
votes
4answers
3k views

Pgrep returns mutiple PIDs

My script accepts a process name as an input and kills it, I tried using pgrep but its returning two PIDs , one for the process which is running and one for the script which accepts the process name ...
4
votes
4answers
2k 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 ...
13
votes
4answers
13k views

How can I use bash's if test and find commands together?

I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command. The log files are stored in this format: /var/log/crashes/app-2012-08-28.log ...
3
votes
1answer
209 views

Locking in a shell script

I want to write a long-running shell script so that only one copy could be run at a time. If the script crashes, I want a new invocation of the script not to be stopped by a lock from the crashed ...