The shell is unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks. Use this tag for questions applying to /bin/sh and most compatible shells (ash, bash, ksh, zsh, …).

learn more… | top users | synonyms (1)

0
votes
2answers
246 views

Can not execute command with root in a remote Linux host

I try to migrate shell code to Python fabric code. The issue I meets is that Python can not execute command with root in a remote Linux host. Could anyone meet this issue and figure it out? I am very ...
0
votes
0answers
33 views

Shell script to kill process using more than X %cpu after X time

I am in need of a sh script that will auto kill a process that is using more than X %cpu after X minutes. I did some search but none of the scripts I found work. Can anyone help me? Thank you!
2
votes
1answer
20 views

Script to parse fields

I need some help in parsing a file & extract certain fields from the same : Sample file format : "make":"honda","group":"xyz","model":"accord","year":"2007","miles":"67000" "make":"honda","group"...
3
votes
1answer
132 views

Assignment to variables

I was trying to devise a solution to a different question asked on the site and came across this curious issue. Here is the set of commands that I issued: $ foo=82a9948422 $ echo $foo 82a9948422 $ ...
25
votes
2answers
8k views

recursive statistics on file types in directory?

I did a website scrape for a conversion project. I'd like to do some statistics on the types of files in there -- for instance, 400 .html files, 100 .gif, etc. What's an easy way to do this? It has to ...
14
votes
3answers
4k views

Which is more idiomatic in a bash script: `|| true` or `|| :`?

I don't do terribly much shell scripting, so I was a little surprised when I was reading the documentation for git submodule and I saw the syntax they used in this documentation: A non-zero return ...
3
votes
5answers
626 views

Copy Sequential Files

I have 12 files named dmp_000, dmp_001, etc, all the way to dmp_011. I would like to copy all of them to rdmp_000, rdmp_001, and so on, sequentially so the numbered files coincide. There must be an ...
8
votes
6answers
1k views

Semi-asynchronous pipe

Assume I have the following pipe: a | b | c | d How can I wait for the completion of c (or b) in sh or bash? This means that script d can start any time (and does not need to be waited for) but ...
1
vote
1answer
150 views

set -A command not running in Linux

if [ $UNITS = day ]; then while ((OFFSET > 0)); do if (( OFFSET >= day )) ;then month=$((month - 1)) if (( month == 0 )) ;then year=$((year - 1)) ...
3
votes
3answers
554 views

How do I write a script to count the total number of files, and directories in my home directory then display it?

Question is in the title been trying to do this. I want to do it with a for loop. This is what I've come up with so far which does not work. for home in /home/ {.,/}*; do echo "$home"; done I would ...
6
votes
4answers
440 views

How can I handle SIGINT trap with a user prompt in shell script?

I am trying to handle SIGINT/CTRL+C interrupt in such a way that if a user accidentally presses ctrl-c, he is prompted with a message, "Do you wish to quit?(y/n)". If he enters yes, then exit the ...
2
votes
2answers
134 views

Help with script reading numbers from a file and determining if they are even or odd numbers

So I want my script to test for the existence of a file which I already populated with numbers. It does that, but after it does that I want it to run a while look checking to see if the numbers in the ...
1
vote
3answers
552 views

How to extract XML from a text file

I have a text file as below. 1001=<?xml ns=dkfj><home><taga>dkjf</taga></home>1002=<?xml ns=dkfj><home1><taga>dkjf</taga></home1> I need ...
2
votes
1answer
144 views

Bash shell launched by forkpty() spawns child processes that ignore SIGINT. Why and how do I ensure SIGINT is not ignored?

I have a C program that uses forkpty to execute a bash shell. I'm finding that the programs launched by this shell are launched with SIGINT ignored, so when I send a Ctrl-C to the shell they never ...
0
votes
1answer
84 views

grep a variable within an if statement

Maybe I am approaching this the wrong way, so I am open for suggestions. I am trying to avoid adding the output of INSTALLED_LIST to a file. I wish to use it within a variable. Basically, I want to ...
2
votes
1answer
85 views

Passing a set of parameters to a program using a Bash variable

I'm trying to clean up my shell script by placing repeating parameters in a bash variable. The best approach I know is to put it in an array and pass it... but that doesn't seem to work. How can I ...
6
votes
1answer
1k views

Can you change timestamp format for history command on tcsh

When I run history command (on tcsh), it only prints the TIME portion of the timestamp and omits the date: 3 15:07 echo $PATH 4 15:07 ls -la In bash, you can use HISTTIMEFORMAT ...
2
votes
3answers
1k views

Select default binary to use

This is the output from which -a autoconf: /usr/bin/autoconf /usr/bin/autoconf /home/amumu/root/bin/autoconf I want to use autoconf in my home directory since it's newer version compare to the ...
0
votes
1answer
104 views

Split files using awk and generate the results in another directory

I am using a shell script where I split a file according to the values of a certain column (using awk). My script is in a folder X, though, and I need the resulting files to be created in a folder Y. ...
1
vote
1answer
217 views

Take text “screenshot” of current terminal contents

I would like to, every 30 seconds or so, copy all text of a certain terminal or terminal emulator to a file, and display it in conky. I'm not talking about simple redirection (command > file), ...
1
vote
1answer
41 views

display users that logged in since a date using last

I am trying to display the user name of the accounts that have logged in during the past month using the last command. At the moment I have this last | awk '{print $1, $4 ,$5 ,$6}' | grep -B 10 Jul |...
1
vote
1answer
35 views

Search for file in folder1 and execute a script if that file isn't in folder2

folder1 contains only .flv files. folder2 contains only .mp4 files. All .flv files in folder1 SHOULD have a corresponding file ending in .mp4 in folder2 : /folder1/123.flv and /folder2/123.mp4. I ...
3
votes
3answers
88 views

Filter a list of files based on file attributes

With find you can search by file attributes like -type, -executable etc. How to achieve something similar, but from an existing list of files? e.g. the output of dpkg -L <package_name>. A pipe ...
0
votes
0answers
134 views

Escape special characters in password on remote machine

I'm trying to run sudo -i on the remote machine and send password in the same line. Faced with the problem of when the password contains special characters; escaping does not help. I would be glad ...
2
votes
1answer
152 views

How to restart a background job failed due to “broken pipe”

I had a background process running, of which the command starts with "nohup nice". As it takes usually 20 hours, so I had it running during the night. However, I got a message says "write failed: ...
1
vote
3answers
110 views

Formatting from Decimal to Exponential

I have a data file abc.txt in the following format: BALT 1 54.500 -161.070 3.95863757 0.01691576 BARM 2 -9.200 67.120 4.07529868 0.01951653 BKSR 3 43.830 142.520 4.08919819 0.00587340 I ...
13
votes
2answers
981 views

How can variables use “~” for cd'ing?

How can I use a variable - $BASE in my cd. I tried the following but I get an error $ cd ~/z/repo_1_ruby_193/ 23:23:57 durrantm Castle2012 /home/durrantm/z/repo_1_ruby_193 $ BASE="~/z" 23:24:03 ...
16
votes
5answers
50k views

Difference between 2>&1 > output.log and 2>&1 | tee output.log

I wanted to know the difference between the following two commands 2>&1 > output.log and 2>&1 | tee output.log I saw one of my colleague use second option to redirect. I know ...
5
votes
2answers
264 views

How to launch gedit from terminal and detach it (just like “subl” command works)?

To open a file to edit in gedit I run gedit sample.py &. But with Sublime Text it is simply subl sample.py. This opens the file to edit and it doesn't run in the background (in my shell). How ...
1
vote
2answers
1k views

Creating directory with space in name [duplicate]

I'm trying to create a directory which has space in name e.g. "user test" when I fire mkdir -p "user test" it works for me. When I put "user test" in variable mkdir fails, it creates "user and test" ...
6
votes
5answers
15k views

How to exit sh program?

I bungled the commands and wrote sh -man Now I've entered a program called sh-3.2 that is seemingly impossible to exit. Ctrlc,Ctrlz, or Ctrlxdoes not work. exit, quit, q, :q also does not work. ...
1
vote
2answers
70 views

Reordering a file, based on a second file

I have a file that is out of order.I would like to reorder it by using a second file. File 1 1 HD;BSkyB:11097:VC23M5O25P0S1:S28.2E:23000:2305=27:2307=NAR@4;2306=eng@106:2308;2309=eng:0:21000:2:2066:...
2
votes
3answers
2k views

How can I sudo su and change directory just after?

I would like to create an alias that does something like this: alias userYYY='sudo su userYYY; cd /a/path/that/only/userYYY/has/access' So then from my command line, I am logged in with a sudoer ...
6
votes
2answers
3k views

What does “cd &” mean?

I know how to change directory from terminal by typing cd <directory> but here's the scenario. I was using terminal to go on my git repository's folder. glennRepositories glenn-remdroid-...
2
votes
2answers
128 views

Why doesn't SIGSTOP work inside screen session?

Consider the following script: #!/bin/bash OFILE='log' echo 'ok' > ${OFILE} kill -SIGSTOP $$ echo 'after stop' > ${OFILE} In an interactive shell the script is stopped and the output is ok. ...
1
vote
3answers
80 views

Find specific class name in a file [closed]

I have a Java file which has a class names as below: class HelloWorld { public static void main(String[] args) { // Prints "Hello, World" to the terminal window. System.out.println("Hello, ...
7
votes
3answers
113 views

'switch' based on shell

I would like to source (i.e. not call) a script from any shell (bash/csh are the primary targets, but fish, zsh, ksh, and rc would also be interesting). I would like if the script can be a single ...
5
votes
3answers
3k views

New line in bash variables

I'm trying to store multiple lines in a bash variable, but it doesn't seem to work. For example, if I list /bin one file per line and store it in $LS, then I pass $LS as stdin to wc, it always ...
0
votes
1answer
222 views

Email issue with output of bash script blcheck [duplicate]

Bash script blcheck from github format issues with emailing output Console output displays correct, but when sent to email via mailx will on go as attachment, not to body. 33%% dnsbl.inps.de ...
1
vote
2answers
159 views

(Shell Script) Variable not pass correctly when fetching value from file?

I have this section of curl command inside my shell script which caused a error 500 repsonse from the server. However, this code works when I manually run the command. curl -qgsSkH "Content-Type: ...
-3
votes
1answer
25 views

What is the correct expression to generate the correct result? [closed]

This script takes a CSV file and iterates over it to create a MD array. It then takes the third column and compares each variable to determine if it is greater than 64. I can get it to work if I use ...
1
vote
0answers
40 views

Why could sudo be required OUTSIDE of shell script but not INSIDE of shell script? [closed]

Using the command xmodmap -e "keycode 66 = Hyper_L" in the console requires sudo. Putting the command xmodmap -e "keycode 66 = Hyper_L" inside a shell script does NOT require sudo (that is, I don't ...
1
vote
1answer
79 views

tail: illegal option — -53\r error while using head and tail on same file in single script

I am using a=`head -1 Rt.in` to get first value from my file and I am saving it as a variable then I am using same variable to tail -$a to tail last lines from my input. But it is giving an error:...
2
votes
2answers
169 views

Arrays in Zsh. Check if any of the following variables is not defined

Say we have a set of variables that we want to test if they are defined, and set a condition if any of them are not. I am thinking of proceeding with something like the following: #!/usr/bin/env zsh ...
12
votes
4answers
29k views

How do I tell if a folder is actually a symlink and how do I fix it if it's broken?

How do I tell if I have a symlink, a shortcut of some sort, or a folder in Linux? Please see following screenshot. As you can see, the ls command lists objects where some are colored in dark blue ...
0
votes
1answer
40 views

Display non-printable parameters in running processes

ps aux displays commands running on the server, but it does not display non-printable parameters in any sort of way that is useful. mycmd.sh: #!/bin/bash while true do sleep 5 done ...
-1
votes
1answer
41 views

$* \ ||, eval and "\; mean?

Could you please explain the below code? What does $* \ ||, eval and "\; mean? Here below are the functions Initialization_variables error_message quit_error_chk Initialization_variables $* \ || ...
1
vote
2answers
44 views

Ignore first x lines when executing a script?

I'm making a script that eventually starts over by executing itself again using exec. The problem is the first few lines are only supposed to be run everytime you launch the script yourself, so I'm ...
1
vote
3answers
99 views

Printing a line at a specified offset in a file

I have a file with 50 characters on each line and 50 lines. Each line of the file contains arbitrary letters. I want to specify a character sequence number (ex: 52) and have the appropriate line of ...
0
votes
2answers
70 views

grep between two lines with specified string [duplicate]

I have this simple plat file (file.txt) a43 test1 abc cvb bnm test2 test1 def ijk xyz test2 kfo I need all lines between test1 and test2 in two forms, the firte one create two new files like ...