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

learn more… | top users | synonyms (2)

4
votes
2answers
421 views

Script working on Mac but not Ubuntu

The script executes on a Mac machine and creates the output file, although on an Ubuntu machine it generates error message. Bash shell is used on both instances.: 1 - /var 2 - /etc : 1 : bad ...
4
votes
3answers
310 views

Dash equivalent of self-redirection of script output

In Bash you can redirect all future stdout output of the currently running script. For example with this script, exec > >(logger -t my-awesome-script) echo 1 echo 2 echo 3 This will end up in ...
4
votes
3answers
411 views

Better way to run single for loop instead of two for loops

This works but the way I did this is kind of silly. Is there a better way to do this? for e in $(ipcs | awk '{print $2}'); do [[ "$e" = "Semaphore" ]] && break echo $e ipcrm shm ...
4
votes
2answers
111 views

Problem with read variable from AWK

I'm trying to get memory info by this command: #!/bin/bash set -x cat /proc/meminfo | grep "MemFree" | tail -n 1 | awk '{ print $2 $4 }' | read numA numB echo $numA I'm getting this + awk '{ print ...
4
votes
2answers
221 views

Spliting `File` and `directory name with space' via shell script

I've got a file called Files.txt having following content: TestApp/Resources/Supporting Files/main.m TestApp/Resources/Supporting Files/AppDelegate.h TestApp/Resources/Supporting Files/AppDelegate.m ...
4
votes
3answers
6k views

Range of the command cut in unix

I'm trying to make a shell script, I want to cut a string using the 'cut' unix command, as follows: namecmpaux=$(echo $namecmp |cut -c0-19) But when I run the shell tells me the following error: ...
4
votes
5answers
388 views

Join two files, matching on a column, with repetitions

How can I get two files A and B, and out put a result like this: File A: 001 Apple, CA 020 Banana, CN 023 Apple, LA 045 Orange, TT 101 Orange, OS 200 Kiwi, AA File B: 01-Dec-2013 01.664 001 ...
4
votes
5answers
1k views

Is there a way to listen to process?

I want to know if there is a way to listen to a process in linux & unix - when it ends and what's the exit code of the process(script). I dont want to write a script that will run X seconds and ...
4
votes
1answer
2k views

Use the terminal's autocomplete path feature for input to a shell script

I want to make a script that: (1) Gets a path giving the user the ability to use tab to autocomplete, then (2) gets a filename from the user, and then (3) creates a file at that path with extension ...
4
votes
3answers
1k views

Reading a binary file as an array of bytes or 16 or 32 bits using shell script

I am working on ARM-based modem Linux host machine and I need to read a bin file as an 8-,16- or 32- bits array. I basically need to encrypt the file in question and was thinking of just XOR-ing the ...
4
votes
4answers
3k views

Make cp return an error value if the target exists

Is there a way to make cp (from GNU coreutils on Linux) return a nonzero value in case the target file does already exist? Or is there any other small utility which is commonly available and which ...
4
votes
2answers
12k views

Bash shell script to locate and remove substring within a filename

I am trying to write a bash shell script in Mac OS X 10.6 (which is called by an Automator Action) to rename TV show DVD rips that I have named badly over the years. I want to remove part of the text ...
4
votes
4answers
177 views

How to Check the existence of multiple files in a directory and zip the same

I am facing an issue while checking the existence of files ending with the same string/pattern. The file names might be as follows abs_123.unl xyz_123.unl Currently my code is as below and working ...
4
votes
5answers
357 views

How to delete files from a folder which have more than 60 files in unix?

I want to put a script in cronjob which will run in a particular time and if the file count is more than 60, it will delete oldest files from that folder. Last In First Out. I have tried, ...
4
votes
1answer
93 views

Why does 'jobs' always return a line for finished processes when run in a subshell within a script?

Normally, when a job is launched in the background, jobs will report that it is finished the first time it is run after the job's completion, and nothing for subsequent executions: $ ping -c 4 ...
4
votes
1answer
157 views

Can I refer to a global variable even if a locally scoped one exists with the same name?

If I have a variable x total=3 and then a function function A () { local -i total=0 } Is there any way I can update the 'global' total variable total from within function A ?
4
votes
1answer
137 views

cmd2 `cmd1` vs cmd1 | xargs cmd2

I'm trying to find a file with find and then pass the found path to sqlite3. I'm only dropped into the sqlite> prompt if my command is of the form: sqlite3 `find . -type f -iname "*.db" | head ...
4
votes
2answers
107 views

Is there a way to have a function in my bash script auto run on any command error?

I'm writing a shell script that needs to do a bunch of commands and every command depends on every previous command. If any command fails the entire script should fail and I call an exit function. I ...
4
votes
3answers
220 views

How to convert an unsigned decimal (less than 1<<32) to 4 bytes (binary) in bash?

Example: $ decimal=$(( RANDOM << 17 | RANDOM << 2 | RANDOM >> 13 )) $ echo $decimal $ printf "%08x" $decimal | xxd -r -p >bytes The result is in bytes file. Can it be done in ...
4
votes
3answers
9k views

Script to ssh and run a command doesn't work

Below is the script. I wanted to login several servers and check for the kernel version. #!/bin/bash #input server names line by line in server.txt cat server.txt | while read line do sshpass -p ...
4
votes
2answers
4k views

Copy ssh public key to multiple Linux hosts

I am trying to copy .ssh/id_rsa.pub from our central server to multiple servers. I have the following script which I usually use to push changes to the different servers. #!/bin/bash for ip in ...
4
votes
2answers
575 views

Shell script (bash) to add or replace the license text contained within .c , .h and makefiles?

I have a set of *.c , *.h and Makefiles in a folder where some files contain the license text and some files don't have any license text. So I need a shell script where I can add license text if the ...
4
votes
2answers
3k views

Is there a GUI debugger for shell scripts

Debugging bash scripts on command line is complicated. Is there a good GUI based debugger available for debugging bash scripts?
4
votes
2answers
4k views

Bash's conditional operator and assignment

Can we use bash's conditional operator with assignment operators after colon? Bash reference manual explains the arithmetic operators as follows. conditional operator expr ? expr : expr assignment ...
4
votes
2answers
1k views

Script to loop through folders with numeric names

I am working on a bash script to compress the images in my Wordpress folders. The wordpress folder structure is as follows: > wp-content/uploads/2014/01/filename.jpg > ...
4
votes
3answers
243 views

shell script problem with using the operators

I am comparing two strings using the levenshtein distance algorithm. I got the PHP implementation of the algorithm from here. I am calling this php implementation from my shell script as below. ...
4
votes
2answers
389 views

Dump process's stdin and stdout

I have two processes, let's say Parent and Child. Parent launches the Child and communicates with it through child's stdin and stdout. Parent <-> Child These processes use text protocol and I ...
4
votes
3answers
1k views

Help Shell script to pass variables to rsync

I'm trying to create a simple wrapper shell script for rsync. When I send file names to my script, rsync can never seem to identify the correct location. The file names have spaces in them. I've tried ...
4
votes
2answers
3k views

Insert to oracle table in a loop

Requirement: In one server in a directory multiple files are present. I need to read the file names and populate into a oracle table. I have written the script like this, but its not working: ...
4
votes
2answers
687 views

Create sub-directories and organize files by date from file name

I have been using the following script to organize my photos into "Date' Directories: for x in *.JPG; do d=$(date -r "$x" +%Y-%m-%d) mkdir -p "$d" mv -- "$x" "$d/" done This script works ...
4
votes
3answers
2k views

using sed to replace pattern with hash values

I want to search the file and replace specific pattern with its hash (SHA1) values. For example, let file.txt has the following content: one S56G one two three four five V67X six and I want to ...
4
votes
3answers
16k views

Loop over a file and read values from two columns into variables

I have a file (data.dat) which contains 3 columns. I want to read in two of them with the shell script and fill the contents into a template file. The file looks like this: 12 180 2390 14 177 2210 16 ...
4
votes
2answers
28k views

Assigning the output of a SQL query to variable

I am connecting to oracle database and firing a query and assigning the output to variable But when I echo the value of the variable it doesn't get printed correctly. count=`sqlplus -s ...
4
votes
3answers
299 views

Back up logs to a new directory

I need help to write a script to copy log files. There are two format of logs: System_<date_timestamp>.log trace_<date_timestamp>.log New files are generated when trace log reaches ...
4
votes
2answers
2k views

Specifying a generic interpreter for a program like expect?

I am writing expect script which can be used on mulple operating systems but the problem is i can't use #!/usr/bin/expect evreywhere so instead i tried to do #!`which expect` at the top but it ...
4
votes
2answers
10k views

list all files newer than given timestamp and sort them

I want to list all files (sorted by date) that are newer than timestamp in format 20130207003851 in directory /tmp only. Subdirectories can be omitted. Using SUSE Linux Enterprise Server 11. The ...
4
votes
2answers
8k views

How to remove trailing whitespace at the end of the line in given files (more than one)?

I'm sooo fed up with useless spaces in source files that I've even configured vim to show them. The problem is that I'm very often (if not always) have to deal with useless spaces with my mates ...
4
votes
3answers
5k views

How to write a shell script that gets executed on login?

I am trying to write bash shell script in Ubuntu 11.10 Linux distro, that will get executed automatically on logging into the system. But I am not able to figure out that what to write in script that ...
4
votes
3answers
1k views

What's the best way to determine the working directory for UNIX process using 'ps'?

I am running a Ruby command line script (rufus.sh)which ultimately calls Thread.new, which spawns a UNIX process as shown below. I run this script for more than 1 directory as the output of the ps ...
4
votes
2answers
543 views

Shell script printing the most visited directories

Is there a way to write a bash script with the following functionalities? Be launched when I press some key or key combination. (not so important requirement) Identify the 7 most visited directories ...
4
votes
3answers
281 views

Best way to work through / display a tree of images sorted by size

I've got a deep directory tree containing .PNG files. I'd like to find all the .PNG files in the directory, sort them in order of size from smallest to largest, and then display every 50th image. ...
4
votes
3answers
56 views

Go from a String to an Array of “words” in Bash

I need to go from a string to an array where each entry is each word on that string. For example, starting with: VotePedro="Vote for Pedro" I need the array: Vote For Pedro Which I should then ...
4
votes
6answers
243 views

Loop in text files shell script

I have a list of files in one directory and a set of jpegs corresponding to each file in another directory. I need to loop over all files, and for each file name, determine the target directory. For ...
4
votes
1answer
55 views

Settings environment variables from a file

I want to have a file (outside of git) that contains certain environment variables. While a script (which is in git) needs to read them and export to env. My current implementation works for ...
4
votes
4answers
525 views

How can I copy a file in a bash script and rename it while copying and place it in the same directory

How would I copy a file "file.doc" and rename it while copying to "file_copy.doc" and place it in the same directory ? And this only by calling the script and adding the file name in the argument: ...
4
votes
2answers
464 views

Nested 'awk' in a 'while' loop, parse two files line by line and compare column values

I need some help with a combination of awk & while loop. I have two simple files with columns (normal ones are very large), one representing simple intervals for an ID=10(of coding ...
4
votes
2answers
139 views

Shell script error handling while assigning STDOUT to variable

I am trying to implement error handling in my shell script as described in the 2nd answer in Best practice to use $? in bash? My script looks like this: #!/bin/bash try() { "$@" code=$? ...
4
votes
1answer
203 views

How to catch a signal in command line?

I asked this question. SIGPIPE signal is generated to stop the execution of the command as told in the answer. But how do I capture this signal and gracefully terminate the command? The command exits ...
4
votes
1answer
632 views

Bash script for scp interpreting tilde (~) too soon

So, I just started a new C++ class that requires me to program in a Linux environment. I don't have Linux installed on my machine, but I do have OS X and I can ssh into a linux machine in the lab at ...
4
votes
1answer
4k views

Bash Script Permission denied & Bad Interpreter

I'm on a kali linux 64 bit. I have created a python script which takes 2 arguments to start. I don't want to type out every time the exact same paths or search in the history of the commands I used ...