Hot answers tagged

13

The shell stores the exit value of the last executed process in the variable ?. You can assign its value to one of your own variables like this: check=$? If you want to act on this value, you may either use your check variable: if (( check )); then # do things else # do other things fi or you could skip using a separate variable and having to ...


9

As you've noticed, the file is created before ls is run. This is due to how the shell handles its order of operations. In order to do ls > file the shell needs to create file and then set stdout to point to that and the finally run the ls program. So you have some options. Create the file in another directory (eg /tmp) and then mv it to the final ...


8

The output file is created by the shell before ls begins. You can get around this by using tee: ls | tee list To thoroughly defeat any race condition, there is always ls | grep -vx 'list' > list Or if you like that tee displays the results as well: ls | grep -vx 'list' | tee list However, as pointed out in comments, things like this often break ...


7

cd is a shell builtin, and sudo only works with programs. Try using either su - or sudo -i before changing directory. These will elevate your login session to that of the root user. Once finished making changes, make sure to type exit to return to being a normal user.


7

Well, if you know your IP starts with 10.16, it's trivial: ifconfig -a | grep -oP '\b10\.16\.[0-9.]+\b' Or, if your grep doesn't support -P or -o: ifconfig -a | awk '/10\.16\./ && /inet/{print $2}' If not, you could find all lines starting with inet and print their second field: ifconfig -a | awk '$1=="inet"{print $2}' That, however, would ...


6

First of all, redirections can occur anywhere in the command line, not necessarily at the end or start. For example: echo foo >spamegg bar will save foo bar in the file spamegg. Also, there are two versions of which, one is shell builtin and the other is external executable (comes with debianutils in Debian). In your command: which lsb_release 2>...


6

They're not completely identical. In cases where it matters, the redirection approach will generally give more annoying and obscure results (of course that might be what you want though). $ cat < /proc/self/maps $ cat /proc/self/maps 55c61257e000-55c61258a000 r-xp 00000000 fd:00 1180143 /usr/bin/cat ... Or try grep, the search ...


6

You can use udevadm to get this information. For example on my system lspci gives me: # lspci|grep VGA 01:00.0 VGA compatible controller: NVIDIA Corporation GK106 [GeForce GTX 650 Ti Boost] (rev a1) Querying udev instead I get: # udevadm info -q property -p /sys/bus/pci/devices/0000:01:00.0 DEVPATH=/devices/pci0000:00/0000:00:02.0/0000:01:00.0 DRIVER=...


6

Try: awk '{f=1} $4 ~ /^192.168/{f=0} $4 ~ /192.168.(125.100|126.100|155.240)/{f=1} f' file Example Consider this test file: $ cat file Jul 13 21:47:41 192.168.100.254 "user from 192.168.100.101" Jul 13 21:47:41 192.168.125.100 "user from 192.168.100.101" Jul 13 21:47:41 192.168.126.100 "user from 192.168.100.101" Jul 13 21:47:41 192.168.155.240 "user ...


5

cat's job is it to concatenate everything it gets and then to print it out on stdout yet often is used to print out the contents of a single file (which of course is just concatenating the contents of all the one files given to it and then writing it to stdout). stdout (standard output) is just programs usually write output data. (There are programs (like dd)...


4

The command is sudo. Add a line such as below into /etc/sudoers sigis ALL=(ALL) NOPASSWD: ALL This means user sigis can now run things like the command below without requiring password. sudo shutdown -h now


4

You have three options: press controlS to stop output, controlQ to resume (this is called XON/XOFF) redirect your output to a pager such as less, e.g., strace date | less redirect your output to a file, e.g., strace -o foo date, and browse it later.


4

A little bit confusing regex for sed but workable sed ' :a #mark return point s/\(\(^\|)\)[^(]\+\);\s*\([^)]\+\((\|$\)\)/\1\n\3/ #remove ; between ) and ( ta #repeat if substitute success s/[[:blank:];]\+$// #remove ;...


3

From your output, it's a redhat based system. Your kernel has el6 in the name and gcc states Red Hat 4.4.7-16. This more than likely means it's CentOS 6. Typically on Red Hat systems, these will give you a hint on what's installed: /etc/redhat-release /etc/centos-release uname -r => If the kernel has an EL* in the name, it's Enterprise Linux. The number ...


3

You might need to specify the subnet mask to use. The command above is likely assuming that the subnet mask is 255.255.255.255, which is for a point-to-point network. The following might work: sudo ifconfig wlp3s0 down sudo ifconfig wlp3s0 192.168.1.12/255.255.255.0 sudo ifconfig wlp3s0 up (Also check to see that a default route is present, using the ip ...


3

You can't, as it has been already said, if the files have been compressed with standard gzip. If you have control over the compression, you can use dictzip to compress the files, it compresses the files in separate blocks and you can decompress just the last block (typically 64KB). And it is backward compatible with gzip, meaning the dictzipped file is ...


3

for i in {b,c,d}; do cp /path/to/directory/a.txt /path/to/file/$i.txt; done


3

You can make the filename temporarily hidden: ls >.list && mv .list list


3

You can use moreutils sponge: ls | sponge list Or with zsh: cp =(ls) list With GNU ls: ls -I list > list (though if there had been a file called list before, that means it won't be listed). Since ls output is sorted anyway, you can also use (assuming your filenames don't contain newline characters): ls | sort -o list Or to avoid the double ...


3

You can use shell globbing for this: cp -rp *bat*/ /destination/ Here *bat*/ will expand to directories having bat in their names. Or using find, which will work even if there are so many files that you get an error because the command line is too long: find . -maxdepth 1 -type d -name '*bat*' -exec cp -rpt /destination {} +


2

+1 to what @jos said above ("google it"). In this particular case you should remove the ".rela_" and "_gpl" parts. You'll find that these are kernel symbols. "rela" is an acronym for "relocation" and has to do with the ELF file format. These are all interesting topics and I encourage you to look into them further.


2

You can do that with bash itself, using command substitution and then parameter expansion. First take the output of the command in a variable by using command substitution $(), and then use parameter expansion to replace all newlines with spaces ${variable//$'\n'/ }: $ myVariable=$(grep "type" /root/myFile | cut-d'=' -f2) $ myVariable=${myVariable//$'\n'/ ...


2

The syntax of the command which lsb_release 2&>1 /dev/null is wrong. You probably meant to run which lsb_release 2> /dev/null assuming you're running an "sh" type shell. If you're running csh type shell then you have more problems with stderr redirection.


2

I'm assuming you're not very familiar with Unix yet, or you wouldn't ask this, so I'm going to include the thought process by which you'd arrive at a command like this. You'll be able to find the next one on your own then. First, you need to get a list of running processes. To list the running processes, you use ps. You probably already know that, but it's ...


2

The most common used idiom in Unix to separate the command line flags from the rest of the arguments is to use a double dash (--). If the utility uses the C library function getopt() (either directly or indirectly), the double dash will signal the end of the command line options, and getopt() will end its parsing of the command line. This is from the ...


2

If your goal is to monitor the system, you want pam_tty_audit. As the name implies, pam_tty_audit is a pam module which when configured properly, is invoked any time a user opens a session (and gets a TTY). The module records all input & output, and sends everything it records to the auditd daemon. You can then execute queries against the auditd daemon ...


2

If you really want to, you could make a program (or shell script) which calls script writing to a timestamped "typescript" file (and in turn calling your real shell) and make that program your default shell in /etc/passwd. There are a few pitfalls: you may have to add this program to /etc/shells doing this sets the SHELL environment variable, which is ...


2

I'm not sure why your dhcpcd isn't using syslog, as that's the default behavior of the utility (maybe Void Linux is using a special version). Anyway the output is going to the console because that's where the init manager sends its output, and unless redirected, applications started by the init manager inherit STDOUT & STDERR. Meaning the fix is to ...


2

You've told bash to set the variable check=grep in the environment it passes to the command -ci 'text' file.sh but ci does not exist. I believe you meant to enclose that command in back-ticks, or in parentheses preceded by a dollar sign, either of which would assign the count of how many lines 'text' was found on (case insensitively) in the file: ...


2

Your question is unclear but based on the code you’ve submitted, it looks like you want the variable check to store the exit status of the grep command. The way to do this is to run grep -ci 'text' file.sh check=$? When running a command from a shell, its exit status is made available through the special shell parameter, $?. This is documented by POSIX (...



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