Tag Info

New answers tagged

2

From the Upstart cookbook, Changing the Default Shell. There are 3 options, the first 2 involve changing your default shell from /bin/sh to something else. But the 3rd option looks like it would solve your particular issue. excerpt Use a "here document" (assuming your chosen shell supports them) within the Job Configuration Files you wish to run with ...


0

command="grep $regex1 filelist | grep $regex2" echo $command | bash


3

With awk: $ awk 'FNR==NR{a[$1];next} ($2 in a){$2=$2"_terminated"}1' hr.txt empt.txt 21356 suresh 12/12/2012 23511 ramesh_terminated 11/06/2011 31456 biswajit 09/08/2013 53134 archan_terminated 06/02/2009


2

There are some good answers here, but I see only one that incorporates the Val part of the problem, and it’s ambiguous whether that is correct.  I agree that awk is “an amazing tool”, but it’s not necessary here; I believe that this sed command: sed -n '/\[part1\]/,/\[part2\]/s/.*Val.*=//p' "$file" probably does what’s desired.  Like the other sed -e ...


0

export does this far more safely than does eval because there is no danger of it executing shell code following a shell token. But it does export the variables so you can take it as you will. export "filemsg$word1= "


1

To get the whole lines from the first part: awk '$1 ~ /^\[/ {n++;next} n==1' To just print the right hand side of the first =: awk '$1 ~ /^\[/ {n++;next} n==1 {sub(/^[^=]*=[[:blank:]]*/,""); print}'


1

sed -n '/part2/q;s/[^=]*=//p' \ <<\DATA <Title> [part1] A.File = a A.Val = val1 B.File = a B.Val = val1 [part2] A.File = a1 A.Val = val2 B.File = a B.Val = val1 DATA OUTPUT a val1 a val1 That should do the trick. It will immediately quit the input file the first time it encounters the string part2 anywhere in the input. ...


2

and use this: sed -n -e '/\[part1\]/,/\[part2\]/p' FILE |sed -e '1d;$d'| awk -F "=" '{print $2}' OUTPUT is: a val1 a val1


4

You need to use a more sophisticated tool to parse the file. For example, awk: #!/bin/sh getCalibDate() { file="${1}" value=$(awk '/\[part/{a++}(a<2 && /Val/){print $NF}' ${file}) for v in $value do echo $v done } getCalibDate ${1} Here, the variable a is incremented each time a line matches [part. Then, the last ...


8

If you have only 4 lines after [part1] you can use -A4 option with grep: cat ${file} | grep -A4 "part1" | cut -d'=' -f2` For general case (more than 4 lines after [part1]) use sed to get the text between two parts: cat ${file} | sed -n "/part1/,/part2/p" | head -n-1 head is to delete additional part2 at the end As terdon said you don't have to use ...


0

Lukas's script (https://github.com/lkettenb/sound-output-switcher) is handy- It uses the appindicator package, which can be installed with sudo apt-get install python-appindicator


5

Use find which is better suited for your intended purpose: find . -name "mkmf*" It will list all appearances of your pattern including the relative path. For more information look at manual page of find with man find or go to http://www.gnu.org/software/findutils/manual/html_mono/find.html


-2

See the manual: man ls notes that: List information about the FILEs (the current directory by default). So, unless you pass a directory name as an argument, ls is showing you the files in your working directory. To see the name of that directory, you can use the PWD variable (print working directory): printf '%s\n' "$PWD" echo "$PWD"


0

Since you can't connect directly from server 1 to server 2 you can use this, having your local machine in the middle: ssh server1 command | ssh server2 "cat > output.txt"


3

use readlink to get the target of a symlink: TARGET=$(readlink $1) then use the power of shell, to remove everything before the last /; ID=${TARGET##*/} or remove everything after the last /: BASE=${TARGET%/*} then use the power of shell to do simple arithmetic NEWID=$((ID+1)) finally glue them together: NEWTARGET=${BASE}/${NEWID} or, in one ...


2

If command4 is currently running, it is possible to do this pretty straightforwardly: ^Z $ fg && right_command5 && command6 This is essentially what you were already doing to start command4 in the first place. wrong_command5 and the rest will be replaced and never execute. I think that behaviour is going to be unexpected to you, so read ...


0

You can run: ssh remote_server "command" > file_on_local_host.txt or use the output as an input for local command: ssh remote_server "remote_command" | local_command


3

General, you can always do: <command> | ssh user@remote-server "cat > output.txt" It saves output of <command> to output.txt file in remote server. In your case, on Server-1: echo "qwerty" | ssh user@Server-2 "cat > output.txt" If two servers have no connectivity, but you can ssh to both servers, then from local machine, you can do: ...


1

(This answers an earlier version of the question, which was pretty different due to the omission of one character. The second command was fg && command4 & wrong_command5 && command6 originally, and it turned out that it was really meant as fg && command4 && wrong_command5 && command6. This answers the old question ...


0

You can put files in your /etc/cron.d as root (or if you are have sudo). You could do something like this ... # cat > /etc/cron.d/mycronjob <<EOT * * * * * /bin/logger "Hello from cron" EOT ... then you can watch your cron job write the system log like so ... # tail -f /var/log/messages


3

Each user has their own scheduled tasks. If you go and pick your kid from school at 4pm every day, your neighbor doesn't also go and pick your kid at 4pm. Cron jobs can be registered by each user (in which case they run with that user's privileges) or at the system level (in which case they run as a user chosen by the system administrator). Each scheduled ...


1

When you read a whole line with plain read (or read -r or other options that don't affect this behavior), the kernel-provided line editor recognizes the Backspace key to erase one character, as well as a very few other commands (including Return to finish the input line and send it). The shortcut keys can be configured with the stty utility. The terminal is ...


0

In any shell that's compatible with Bourne or POSIX,  redirections are processed from left to right, and the pipe comes first. Thus your command is executed in this way (I omit what happens in subprocess 2): Create a pipe. Fork two subprocesses, 1 and 2. Redirect the standard output of 1 to the pipe. Redirect the standard output of 1 to file. In 1, execute ...


4

Yes, this is job for tee: rpm -qa | tee file | wc -l In this construction a | b a's stdout goes to stdin of b. In case of a > file | b all output form a goes to file and nothing goes to b stdin. tee command make a copy of all it receives on stdin to both file and stdout.


3

Yes, this is a job for tee: rpm -qa | tee file | wc -l Shell redirection (>) is just that — redirection — and you can only point the output stream to one other place at a time. There's nothing left for the | to see at that point. tee is made for just this purpose, where you want to split the stream into two parts, one going into a file and one still on ...


1

rpm -qa > file ; wc -l file should do what you want.


1

read is a bash builtin. (see type read.) You can find documentation with man bash: read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] [...] -e If the standard input is coming from a terminal, readline (see READLINE above) is used to obtain the line. Readline ...


3

Use read -e: $ read -e -n 5 13acX read -e means that: Readline (see Command Line Editing) is used to obtain the line. When you do that, you can edit the input in any of the ways you would when writing at the regular shell prompt, including backspace, Home, and so on.


3

Unquoted variables and command substitutions like $i or $(git …) apply the split+glob operator to the string result. That is: Build a string containing the value of the variable or the output of the command (minus final newlines in the latter case). Split the string into separate fields according to the value of IFS. Interpret each field as a wildcard ...


2

The wildcard pattern is expanded by the shell before the command is invoked. See G-Man's answer for a full explanation. Most shells require some form of intermediate command in order to apply a text transformation to the matches. Zsh offers a way to transform matches on the fly with its glob qualifiers: e or + to execute code for each match, which can ...


-2

You can use it inside a script: if [ `ps -ef | grep "script.sh" | grep -v grep | wc -l` -gt 1 ] ; then echo "RUNNING...." else echo "NOT RUNNING..." fi


2

The point that you may be missing – that many people have trouble with, especially if they have experience with other operating systems before they come to *nix – is that, in many other OSs, wildcards on the command line are normally passed to the command to process as it sees fit.  For example, in Windows Command Prompt, rename *.jpeg *.jpg Whereas, in ...


0

Text streams like this should be read using a while loop rather than for, which is probably causing the issue (although I cannot reproduce it). A simple way to do this: git branch | while IFS= read -r line do echo "${line:2}" done Comparison: $ cd -- "$(mktemp -d)" $ git init Initialized empty Git repository in /tmp/tmp.MJFmu7q7EH/.git/ $ touch ...


0

This doesn't work because the shell tries to expand the entire string */test which doesn't exist yet. By default the string then expands to itself. If you're using Bash you may want to look at the various globbing options to handle this the way you want.


2

The obvious answer, "install the command on the remote machine", is the most clean solution, so we should not ignore it: If this is possible to install the command as root, for example with sudo apt-get install fish, the command can be run like this: ssh remote -t fish The question is about what to do when we can not install a command on the remote ...


4

Install your favorite shell on the remote machine. You don't need any administrator privileges to do that, you can install programs in your home directory, it's just less convenient. See Installation on debian 5 32-bit without being a root, How to install program locally without sudo privileges?, Keeping track of programs and other questions. If you want to ...


2

The basic approach is to copy the shell executable to the remote host using scp then execute it using ssh, e.g. scp /usr/bin/fish remote:fish && ssh -t remote '~/fish' The -t is needed so that ssh allocates a tty, which it wouldn't do by default when executing a remote command. This assumes your remote host is running the same operating system. ...


2

User can use sshfs to mount his remote $HOME on his local machine. In such scenario, user wouldn't use his shell of choice on the remote machine directly but, still, better than nothing.


0

I don't think you can run a shell that is on one machine on another. The only way to run it on the remote machine is to install it.


3

Simple answer: No, you cannot use on a remote box a program that is not installed on the remote box. Workaround: You do not need admin privileges to install a shell on the remote system. You can install it in your home directory but probably you have to compile it from the sources. Typically using something like configure --prefix=${HOME}/local Last note: ...


3

While the bash aspect has been covered, your question makes me think you've come across those variables in perl code. $& and $_ are special variables in perl. And they are especially found in perl code called from the shell code. $_ is the default variable many perl functions and operators work on. That variable is also the default variable set by ...


2

The variant command -v is the defined in the POSIX standard, so it is exactly the "UNIX-like way to do it". For the standard, see POSIX - Shell & Utilities - command For all the details, and still much more details: Why not use "which"? What to use then?


1

Providing & means you are going to run a particular command in the backend or as a job. So that is what the output of echo $& gives.


17

$& is not a single token/special variable, it is simply $ and &. The command echo $& is treated as echo $ &, which echos a literal $ in the background. $_ on the other hand is a special variable that expands to the last argument of the most recent command executed.


1

grep -oP 'name="\K[^"]*' filename Output: What_I_Want_To_Extract See: http://www.charlestonsw.com/perl-regular-expression-k-trick/ Your version adapted: grep -o 'name=".*">' HTMLFILE | sed 's/name="\|">//g' NEWFILE


3

This is covered in Variable expansion: ${parameter:?word} If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted. Therefore if there is no argument with the execution of the ...


3

You need to set the display variable, but this is done as: #!/bin/bash export DISPLAY=":0" gedit and then at now + 1 minute -f test.sh works for me on Ubuntu 14.04.1 LTS


3

If you are using the GNU tools, the following should also work: find /some/path -type f -name file.pl -size +10M -print0 | xargs -0 -n 1 -r perl /my/script.pl Explanation: The option -print0 causes GNU find to separate the file names with \0 bytes. Since \0 bytes cannot be part of the file name, this uniquely separates the file names. The option -0 ...


5

That's what the -exec predicate is for: find /some/path -type f -name file.pl -size +10M -exec perl /my/script.pl {} \; If you do want to have your shell run the commands based on the output of find, then that will have to be bash/zsh specific if you want to be reliable as in: zsh: IFS=$'\0' for f ($(find /some/path -type f -name file.pl -size +10M ...


-3

This should do it for path in `find /some/path -type f -name file.pl -size +10M`; do perl /my/script.pl $path ;done



Top 50 recent answers are included