Tag Info

Hot answers tagged

14

You should never parse /etc/passwd directly. You might be on a system with remote users, in which case they won't be in /etc/passwd. The /etc/passwd file might be somewhere else. Etc. If you need direct access to the user database, use getent. $ getent passwd phemmer phemmer:*:1000:4:phemmer:/home/phemmer:/bin/zsh $ getent passwd phemmer | awk -F: '{ ...


13

Just use the -w flag of the test utillity: [ -w /path/to/file ] && echo "writeable" || echo "write permission denied" Note that if you're going to write to the file later, it's still possible that you won't be able to write to it. The file may have moved, the permissions may have changed, etc. It can also happen that -w detects write permissions ...


12

The easiest way to accomplish what you want, is to delete all files in the directory that are older than 30 days. You can use this find command: find /path/to/log/ -maxdepth 1 -name "NameLog.log*" -mtime +30 -delete Explanation: (see: explainshell) -maxdepth 1: don't go deeper into the folder structure -name "NameLog.log*": applies only on log file with ...


12

Let's see, !a[$0]++ first a[$0] we look at the value of a[$0] (array a with whole input line ($0) as key). If it does not exist ( ! is negation in test will eval to true) !a[$0] we print the input line $0 (default action). Also, we add one ( ++ ) to a[$0], so next time !a[$0] will evaluate to false. Nice, find!! You should have a look at code ...


12

Here is the processing: a[$0]: look at the value of key $0, in associative array a. If it does not exist, create it. a[$0]++: increment the value of a[$0], return the old value as value of expression. If a[$0] does not exist, return 0 and increment a[$0] to 1 (++ operator returns numeric value). !a[$0]++: negate the value of expression. If a[$0]++ return ...


11

Part 1 Simply delete the 13th line: sed '13d' <file.txt And a general way to do the complement of the above is: sed '13!d' <file.txt Part 2 Because it can be done: sed -n ':a;${P;q};N;4,$D;ba' <file.txt Note the 4 is one more than the number you require. So if you wanted the last-10th line, this would be 11. Testing with seq: $ seq 100 ...


5

Your script is a sed script, not a shell script. So you don't need to put sed at the beginning of the line, or put quotes around the commands. Change it to: #!/bin/sed -f s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/192.100.100.100/g


5

From man bash under CONDITIONAL EXPRESSIONS: -t fd True if file descriptor fd is open and refers to a terminal. Assuming fd 1 is standard out, if [ -t 1 ]; then should work for you. The Advanced Shell Scripting Guide claims that -t used this way will fail over ssh, and that the test (using stdin, not stdout) should therefore be: if [[ -t 0 || -p ...


4

expect is for a different purpose. It runs commands on a captive program. You, by contrast, are asking for a way to send commands to a process already running in the background. As a bare-bones minimal example of what you want, let's create a FIFO: $ mkfifo in A FIFO is a special file that one process can write to while a different process reads from ...


4

You can use public key authentication: on the server, add a line with the command run by rsync (for security) and your public key to ~root/.ssh/authorized_keys. See the sshd(8) man page for information on the authorized_keys file format.


4

The shebang line you've seen may work on some unix variants, but not on Linux. Linux's shebang lines are limited: you can only have one option. The whole string -d -m -S screenName /bin/bash is passed as a single option to screen, instead of being passed as different words. If you want to run a script inside screen and not mess around with multiple files or ...


4

Processing arguments Processing arguments that are passed via a script's command line is as simple as follows. Say we had this script: $ cat cmd.bash #!/bin/bash echo "arg1: $1" echo "arg2: $2" Now run with no arguments: $ ./cmd.bash arg1: arg2: With 1 argument: $ ./cmd.bash hi arg1: hi arg2: With 2 arguments: $ ./cmd.bash hi bye arg1: hi ...


4

This script will create a directory for each of the lines in list.txt. It can deal with spaces but not other weird characters (such as \r or\t` and the like, but that shouldn't be an issue here): #!/usr/bin/env bash while IFS= read -r dir do mkdir -- "$dir" & done < list.txt The & sends the job to the background so the script will continue ...


4

The following example counts the times I am mentioned without needing the lastloggedin file: $ last | awk '$1=="yeti" { ++count } END { print count }' 106 If you insist in using or are forced to use the lastloggedin file, you can do it this way: $ last > lastloggedin $ awk '$1=="yeti" { ++count } END { print count }' lastloggedin 106 Use ...


3

you are looking for $myuser's home dir ? awk -F: -v user=$myuser '$1==user { print $6 ;}' /etc/passwd you can use awk -F: -v user=$myuser '$1==user { printf "%s/.ssh\n",$6;}' /etc/passwd to get .ssh dir.


3

slm's answer here hasn't taken into account that you asked about the Korn shell, not about the Bourne Again shell. The (93) Korn shell has no built-in expr command, so when using expr in Korn shell scripts you are using an external expr command. This is not a problem per se. After all, it's how one did things with the Bourne shell, which also had no expr ...


3

You can use the built in arithmetic support in the shell that is used by a double parenthesis preceded by a dollar sign: $(( (first + second + third) / 3)) This is more efficient than expr since it does not require a seperate process (and hence fork, exec, pipe management and return).


3

For said purpose - that is stopping the script at any given line - you could simply use exit to break at this point. For any real life application this would seem a little crude though but for learning purposes it should fit. Say: #!/bin/sh # now I am working here do_something exit # this is old, do not execute did_something exit # this is even older, ...


3

Here's a way to do what you want with a single file (which we'll call $file for now) and print it to standard output # prepend a "# " and remove the .markdown from the filename sed 's/\.markdown//' <<< "# $file" # print a blank line echo # output the file cat "$file" Now for what you really wanted, enclose that in a for loop to iterate over every ...


3

Assuming your shell is bash, this can be a one-liner: perl -i.bak -pe ' /\\begin\{verbatim\}/../\\end\{verbatim\}/ or s/->/\$\\to\$/g ' {Cardiology,Pathophysiology,"Patology and Biopsy",Physiology,Propedeutics,Radiology,Rheumatology,Surgery}/*.tex Note that {...} is a regex quantifier, so the braces need to be escaped. I'd write your code as: ...


3

Well, this one is not exactly the way you want. But still it could be useful for the second option in your question. Install the required packages. sudo apt-get install msmtp-mta Edit the following file to add the details. If the file doesn't exist, you could create it. vi ~/.msmtprc #Gmail account defaults logfile ~/msmtp.log account gmail auth on ...


3

According to the screen man pages: screen -d -m Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts. -S sessionname Set the name of the new session to sessionname. So when I ran the command you provided: screen -dmS name ./script.sh Screen starts a window called name and ...


3

In bash you can test as follows: [[ `tty` = "/dev/pts/1" ]] && echo Access Granted. Welcome. tty will generally output something like /dev/pts/1 or not a tty As you mentioned, apparently in csh you can do as follows: if ($tty == "pts/1") then echo Access Granted. Welcome. endif


3

You could use the -o switch to specify your output format: $ ps -eo args From the man page: Command with all its arguments as a string. Modifications to the arguments may be shown. [...] You may also use the -p switch to select a specific PID: $ ps -p [PID] -o args pidof may also be used to switch from process name to PID, hence allowing the use ...


3

To get you started you can use awk to search for lines in a file that contain a string like so: $ awk '/CFS264/ { .... }' lastloggedin The bits in the { .... } will be the commands required to tally up the number of lines with that string. To confirm that the above is working you could use a print $0 in there to simply print those lines that contain the ...


3

You could try any one of these commands from the Parent folder. If you need to find all the files, find . -print > list_of_files If you just need the directories and the sub-directories inside them as well, find . -type d > list_of_files If you just need directories for single level, find . -maxdepth 1 -type d > list_of_files Now, with ...


3

It's possible that the output is being sent to stderr which is not captured by the > operator which only captures stdout. Instead, if you are using the bash shell, try routing stderr to stdout and into a file using the &> operator. For example: unpackdcm -scr ${in} -targ ${out} &>stat.txt To redirect only stderr, use this: unpackdcm ...


3

The >-sign represents an I/O-Redirection. With >stat.txt you redirect the standard output (stdout) of the application to the file stat.txt. It is redirected, so you will not see any output in the shell. If you want the output in the current shell AND the file pipe the output into tee: your_command | tee stat.txt Or.. your_command | tee -a stat.txt ...


2

To execute a command such as grep or awk, the shell must fork, which means that you get a subshell. The only exceptions are when the command is a builtin or when the command is the last command. But this latter case is just an optimization done by some shells and cannot be done under some conditions that might change the behavior (e.g. existing traps); ...



Only top voted, non community-wiki answers of a minimum length are eligible