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

learn more… | top users | synonyms (2)

14
votes
5answers
1k views

Is this a bug in bash? `return` doesn't quit function if called from a pipe

I'm been having some weird problems with bash lately. While trying to simplify my script, I came up with this small piece of code: $ o(){ echo | while read -r; do return 0; done; echo $?;}; o 0 $ o(){...
4
votes
1answer
243 views

Predicting the PID of previously started SSH command

This is the weirdest thing. In a script, I start an SSH tunnel like so: ssh -o StrictHostkeyChecking=no -fND 8080 foo@bar This starts an ssh instance that goes into the background, and script ...
0
votes
0answers
95 views

Raspberry Pi to Computer - Scripted File Transfer and Deletion

This is my first ever bash script and I've done the best I could to find most of the answers on my own, but I have finally come to a road block. This script seems to (mostly) work, but I am not ...
1
vote
1answer
59 views

AIX Non GNU tool for text replacement

Contents of the file : PS1="$(hostname):${LOGNAME}:\${PWD} ${PROMPT} " PS1="${FGOLD}$(hostname):${LOGNAME}:\${PWD} ${PROMPT} ${NORMAL}" PS1=abd PS1="$(hostname):$LOGNAME:\$PWD $PROMPT " export PS1 ...
0
votes
4answers
486 views

How to skip the first argument in a script

Linux Pocket Guide has a nice example on how to go over all the arguments in a script for arg in $@ do echo "I found the argument $arg" done I am writing a script in which all the arguments will ...
2
votes
1answer
56 views

How to find out if the script is running in Linux or OSX

I want to write a bash script to get some information of Switches through snmpbulkwalk. I would like to use the same script in Linux and OSX environments, so I want to know if there is a way to do a ...
1
vote
1answer
57 views

running a for loop in shell script

I would like to know whats the best way to accomplish this: I have a file with below data: server1 chl(abc.123) chltype(RCVR) chl(xyz.123) chltype(RCVR) server2 chl(abc.000) chltype(RCVR) chl(abc....
1
vote
1answer
214 views

Bash root to user. Best in same or separate script?

I'm working on a Linux Bash script that needs to start as root. At the end of the script, it builds a list of packages. This requires running as user. Then it will move the packages, which needs to ...
0
votes
1answer
192 views

Bash script with ssh not returning value of variables

I'm working on a bash script to help migrate KVM virtual machines, but I'll be using dummy code to explain what is going on. I'm using functions to break up the tasks perform on a remote servers ...
1
vote
1answer
62 views

Linux Mint: make a “public clickable launcher” from a sh root only?

I made a shell script to install packages, configure them, etc. Usually I run it via Terminal sudo sh gizmo.sh Now I aim to put it in a double-clickable file to run it, for any user (no sudo, so su ...
0
votes
1answer
49 views

Folder research task

I have a folder with 3 subfolders containing 6 files each.There are 2 files in each subfolders with :NOPM in their file names. I need to create a shell script that checks all the folders and their ...
1
vote
2answers
122 views

Getting a file with wget when the filename may change slightly

I have a program that takes data from five government sources and merges them into one large database for my company. I use wget to retrieve the files. However I have discovered that one of the ...
1
vote
3answers
131 views

Grep for multiple strings, show number of lines after one of the strings (but not the other)

As the title suggests, I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well. cat file | grep -e 'string1' -e 'string2' works of ...
3
votes
2answers
59 views

Why can't I apply this script to files not in my current directory?

I got this script ~/bin/align-tables from emacs stackexchange #!/bin/sh # -*- mode: shell-script -*- # # tangle files with org-mode # DIR=`pwd` FILES="" # wrap each argument in the code required to ...
1
vote
2answers
186 views

How to remove a line from a tag in shell script?

We have an xml file (abc_lop.xml) in which I need to remove a line which is present in a tag: Below is an xml file, I have shorten it down as it is very big. <HELLO version="4.2" xmlns="http://...
0
votes
2answers
194 views

Replace all occurrences of a string found in a file using values from another file

I have a CSV file (data.csv) as below: apple_val, balloon_val, cherry_val, dog_val 1 ,5 ,6 ,7 3 ,19 ,2 ,3 I have a text file (sentence.txt) as ...
1
vote
3answers
290 views

parse credentials from PHP configuration file

I'd like to write bash script to parse data from a configuration file. I searched for this but without finding something I could modify to suit my needs. Joomla! config file: public $access = '1';...
4
votes
2answers
392 views

Keeping first instance of duplicates

I have a file with multiple columns and have identified lines where specific column values (cols 3-6) have been duplicated using a bash script. Example input: A B C D E F G 1 2 T TACA A 3 2 Q 3 4 I ...
3
votes
2answers
109 views

Shell script to move files between two dirs

I have multiple folders which contain subfolders like JAN/ Jan/ FEB/ Feb/ MAR/ Mar/ and so on. I need to move all files from JAN/* to Jan/ , FEB/* to Feb/ and so on. How do I achieve this with a shell ...
1
vote
1answer
117 views

Echo outputting results in erratic order in BASH

Word of warning: I'm relatively new to BASH scripting, so any advice on my hacky scripting is appreciated. I have a strange issue, in my code I've collected a bunch of error strings from a file in ...
0
votes
2answers
143 views

motd - UNIX - AIX - different welcome screens for diff users

is it possible to segregate different motd files for different users ? So Admin gets a different message and others diff one....
1
vote
3answers
234 views

Set Parent Process ID from ShellScript, kill all Child Processes later

I want to write a shell script for my Ubuntu machine, that starts a few server applications. I need this script in order to execute it from my java application. But I need an easy way to group all ...
1
vote
2answers
270 views

How does this svn script work?

I defined this bash function to update multiple svn projects with a single command: svn_update_all () { repos=`find . -name '.svn'`; for i in "$repos"; do svn update `dirname $i`...
0
votes
4answers
79 views

Getting different parts of a line

Suppose I have a line of text as follows: I have a nice car Is there any way to get different words of the line at different positions individually (say, I want to get 'have' and 'nice' parts) ...
-1
votes
2answers
94 views

comparing a line with its following lines inside a file

Supposing I have a file named sample.txt which contains data in the following format: 1 John 2 Crema 3 Abraham 1 John I can read the file line by line using the following way: while read p do echo ...
0
votes
2answers
35 views

Real path of an included script

Can an included script know if it's being included and get its real path ? Suppose a script called sub.sh that check if the user has access to the script group, before to run its main logic: #... ...
3
votes
1answer
318 views

How to make a sudo passwordless script for any user in bash

I want to be able to run a sudo command inside of a bash script but I was wondering what if I want any user in my server to be able to execute the script without to provide them with my root password, ...
0
votes
1answer
82 views

combining output of grep command [duplicate]

Suppose I have used the grep command : grep 'pattern' 'filename' and have got the result : John Brown Is there any way to concatenate the outputs into a single one like: John Brown And also ...
3
votes
2answers
671 views

missing `]' error in unix shell script [duplicate]

I am trying to run a small script which check for two variables to see if they are empty or not. I am getting the correct output but if also shows me error for missing right parenthesis. I tried using ...
0
votes
2answers
45 views

Unclear where bash output is going

I made a small script to output the list of vim plugin remotes to a file, so I could figure out what ones I want to keep or remove. However, when I run it, the output doesn't get piped to the file. ...
2
votes
2answers
679 views

Find extracted directory name from tar file

I have a project where I am downloading and compiling a bunch of files. The assumptions I can make about this files are: there will only be one top level directory the folder name doesn't ...
2
votes
1answer
177 views

Is it possible to start a command from bash and send a keystroke to it

I'm trying to start Firefox from bash and I want it to open the webconsole on start (this can be done by pressing F12 rigth after it started). Is there a bash command that starts the programm and ...
2
votes
2answers
166 views

How to convert several tiff images into xyz coordinates in a single text file using the terminal?

I have limited experience with programming and Linux and I need your help. The situation I am in is that I have a stack of black and white .tiff images (approx. 400 of 10 Mb each one) which I need to ...
9
votes
3answers
800 views

how to check the format of a filesystem

I would like to check, in a bash script, on what filesystem type a directory is. The idea is something like if [path] is on a [filesystem] filesystem then filesystem specific command end if
1
vote
1answer
270 views

Bash choose option without pressing enter

i have a script like this: #!/bin/bash clear echo -n "Choose an option: 1) Encrypt 2) Decrypt 3) Quit -> " read opzione if [ "$opzione" == "1" ]; then echo -n "Drag here the file to encrypt: "...
3
votes
2answers
421 views

Bash script to display directory listing gives error

I wrote a simple bash script (I'm pretty new to it, you know!) to accept a directory as its argument and print its listing, testing for files and directories. Here's how I approached it: #!/bin/bash #...
0
votes
4answers
496 views

passing the results of awk command as a parameter

Suppose I have used awk to get the value of two different columns: Say the output is: abc def Now, I want to use both abc and def as patterns for grep, using a pipeline with the previous command....
2
votes
4answers
41 views

slicing different words from output

Suppose, the result of a command in linux shell is as follows: X and Y are friends Is there any way to slice each of the words (X, and, Y, are, friends) or the first n words from the result, so ...
0
votes
0answers
51 views

Command line USB event redirect

I help the poor, mentally ill and homeless in my spare time as a hobby. Some of these people have barely functioning laptops with sometimes working track-pads etc. I need a way to programmatically ...
0
votes
1answer
54 views

Move the files from a folder and its subfolder to the same destination folder

I have a script to move files of type .txt to a particular folder. It looks for the files in work folder and move it to completed folder. For example, if there is a .txt file in folder /tmp/swan/...
6
votes
2answers
91 views

direct execution of python scripts

I have noticed that sometimes python scripts are not being started directly, ie /foo/bar.py, but rather from a shell script, which does nothing else than /usr/bin/python -O /foo/bar.py $@ One such ...
0
votes
2answers
43 views

change permissions to a file and everything inside , recursively

So I want to create an alias called changeAllPermisions that accepts one parameter argument in such a way that when changeAllPermissions argument is called , both Group and Other do not have access to ...
1
vote
3answers
26 views

how to edit a bilingual text by scripting?

I have a column of words in which English words are glued to Chinese words like this: abominate******** abhor************* (The stars represent the Chinese alphabet) I want to write a ...
-1
votes
1answer
34 views

getting the pattern from file

I have a text file in the below format. I want to pick one instance each time(line ABC to MNR) and work on the respective lines and columns. Need help to achieve that. ABC 12 20150911 121 005 001 ...
0
votes
1answer
260 views

How do I run the SSH command to set StrictHostKeyChecking=no?

I'm trying to run the command ssh -o StrictHostKeyChecking=no but I'm a bit lost as to how to run it. I've tried running it from a shell script, but the output was the man page or settings that lists ...
1
vote
1answer
226 views

How to kill inactive user logins on Solaris?

On a Solaris 10 server if i do a w, I get this: 12:26pm up 85 day(s), 1:13, 6 users, load average: 0.38, 0.36, 0.38 User tty login@ idle JCPU PCPU what blah pts/1 ...
0
votes
1answer
420 views

could not change directory to “/home/corey/scripts”: Permission denied

I get the error message... could not change directory to "/home/corey/scripts": Permission denied ... when I run the following script ... #!/bin/bash sudo -u postgres psql < setup_dev_db.sql ...
8
votes
3answers
688 views

How would I use GNU Parallel for this while loop?

So I have a while loop: cat live_hosts | while read host; do sortstuff.sh -a "$host" > sortedstuff-"$host"; done But this can take a long time. How would I use GNU Parallel for this while loop?
-2
votes
1answer
2k views

script to check cpu utilization in linux

I want a bash script that show the CPU consumption every minute and save it in a file. The output would be like this: 11/09/2015 10:00: CPU: 60% 11/09/2015 10:01: CPU: 72% 11/09/2015 10:02: CPU: 32% ...
0
votes
2answers
276 views

How to catch carriage return / line feed in case statement

I tried the following to no avail and couldn't find any documentation. read Option case $Option in [1] ) echo "1" ;; [2] ) echo "2" ;; [$'\n'] ) echo "LF" ;; [0] ) exit ;; * ) ...