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

learn more… | top users | synonyms (2)

1
vote
3answers
5k 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
381 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
565 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
2k 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
451 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 ...
2
votes
1answer
515 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 ...
5
votes
2answers
13k 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 ...
6
votes
1answer
9k 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 ...
3
votes
2answers
125 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
678 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
790 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
436 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
413 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
415 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
2k 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
6k 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
666 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
556 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 ...
14
votes
4answers
15k 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
213 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 ...
0
votes
1answer
251 views

How to automatically install packages?

I want to install packages automatically by running a script I can write yum install php, but I want it to install another package once installation of the php package is completed. Is there any ...
0
votes
1answer
221 views

The “cp p2 &2 &” command makes Konsole to disappear, is it a bug?

The goal is start a background process that copies the data from the pipe p2 to the STDOUT as p2 is feed with data by some other process. I guess the command is incorrect. Nevertheless, the Konsole ...
2
votes
3answers
2k views

Switching to superuser while shell script is running

I have a shell script that contains the following lines: CURRENT_USER=${USER} echo $CURRENT_USER su echo switch to `who am i` Once the 2nd line is executed, the shell prompts me to enter the root ...
3
votes
2answers
1k views

How to create multiple zips from a directory containing a million xml files?

I have a directory containing million xml files. I want to create multiple zip files containing n number of xml files. For ex: if n = 10000, then I want to create 100 zip files i.e. each zip file will ...
2
votes
2answers
1k views

How to merge chunks of text files in a directory to one or more files?

I have a requirement to merge chunks of text files in a directory to one or more files and then zip the merged file. For ex: If i have half a million text files in a directory, say A. I want to merge ...
4
votes
1answer
91 views

Transform directory structure

I have the following folder structure root folder |-al2 |- GER.zip |- ENG.zip |-ww |- GER.zip |- ENG.zip and so on. The folder names are random strings. I want to sort the files in ...
0
votes
1answer
389 views

How does “options” in shell scripting work?

I found some code in one of the answers here and accommodated it to my needs but now I have two questions: Q1: how can I display the options text after the code in case statement finishes? So the ...
3
votes
2answers
398 views

rename randomly files on a USB device

I would like to rename all of my files of my USB device randomly. Indeed, in my car, my tuner has not the random function. I think I can do it with PHP ... but I'm pretty sure it is possible to do ...
1
vote
1answer
219 views

Making multiple calls to a program via a bash shell script

Suppose that I have a program make_ndx that outputs via the -o switch. I would like to call myprogram from a bash shell script and output two files, out1.ndx and out2.ndx. (myprogram takes a string ...
1
vote
1answer
2k views

Do shells support recursion?

I'm trying to write recursive functions in my shell scripts. Consider the following code: function printA { if [[ "$1" = 0 ]]; then return else echo "a$(printA $(("$1" - 1)))" ...
17
votes
3answers
28k views

Shell script fails: Syntax error: “(” unexpected

I've been working on a script that automates setting up a development environment for Raspberry Pi development (step by step details that work are here). The script is linked in that article but ...
2
votes
2answers
438 views

Executing zsh rehash after build

I have a build script that can change what binaries are in my $PATH (it doesn't edit $PATH itself, but it adds/deletes files to folders that are already in $PATH). zsh's autocompletion doesn't update ...
2
votes
1answer
155 views

How to have find only search for files in changed directories?

Currently I'm repeatedly doing a 'find' that's too slow. I'm searching for non-hidden executable files within "$root", excluding "$root/bin": find "$root" -type f -perm -o+x -not -path "$root/bin/*" ...
2
votes
1answer
636 views

Extended file globbing not working with cat inside bash script

When, in my terminal, I type cat ~/my/+(a|b)/doc It reads ok from both ~/my/a/doc and ~/my/b/doc, but when I put that command in a bash script: #!/bin/bash cat ~/my/'+(a|b)'/doc I get the error: ...
7
votes
1answer
662 views

Bash script doesn't read input

I have a script that is suppose to run a command in the background, and it does that. The problem is that when the script comes upon a read command, it doesn't pause and accept input. Here it is: ...
2
votes
3answers
994 views

Copy/backup all dirs containing files matching a regexp

I would like to do a backup where I recursively go through a dir structure and only get directories with files whose names matching a particular regexp. I would like to keep the directory structure, ...
4
votes
1answer
1k views

Validate AIX Password for root from script

Looking for a way in a shell script to validate the password for for root on an (AIX) matches a standardized plain text password. This query is similar to another one on this site. I have access to ...
4
votes
2answers
2k views

bash myscript.sh runs in bash, but the first line is #!/usr/bin/sh

I have a script similar following one: #!/usr/bin/sh var="ABC" if [ $var == "ABC" ] then echo True else echo False fi Above code does not work in Solaris Sparc and Solaris X64. It is showing ...
2
votes
7answers
43k views

Adding two numbers using expr

I'm learning Shell scripting for a diploma in IT I'm currently doing. I'm trying to write a small script that adds two numbers as shown as in one of the tutorials we were given. echo "Enter two ...