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, …).
2
votes
1answer
20 views
How do I run two processes and find out when one ends in bash
I was thinking to do something like this:
command1 & command2
wait
Will this work? I want to proceed when command2 finishes, but command1 can still be running.
0
votes
0answers
31 views
Backup And Restore Shell Script
I have been trying to create a script that backups up a certain directory and then restores the directory. The backup works as it should but when I try run the restore function I get the following ...
1
vote
1answer
33 views
How to interactively remove all directories matching given criteria?
My script below has two problems:
The script stops after first matched directory
rm -i is not quite interactive
What are my incorrect assumptions that make it fail? How can I fix it?
ls -1A | ...
2
votes
3answers
141 views
Double square brackets for FreeBSD
I have this code:
[[ "$1" =~ [/\\]$ ]]
On linux it works just fine, but when I tested it on FreeBSD an error occured : ./projekt2.sh: [[: not found and it .. I there any alternative which would ...
1
vote
2answers
38 views
pipe in shell with redirection
My understanding is that a pipe in shell like a | b redirects b's stdin to a's stdout. However, I tried ls | less < somefile, the behavior is very strange. less shows the output of ls concatenated ...
-1
votes
2answers
16 views
Create new file with spaces from argument BASH [duplicate]
I need to create new file which contains spaces in it. And it is passed through the argument. This code below creates 2 sepate files. Can anyone help me how to create 1 file which will contains space? ...
15
votes
2answers
322 views
Where is “export var=value” not available?
I have picked up -- probably on Usenet in the mid-1990s (!) -- that the construct
export var=value
is a Bashism, and that the portable expression is
var=value
export var
I have been advocating ...
1
vote
2answers
29 views
Porting Linux date parsing to FreeBSD
I have date in this format: date -d $datum +"%Y-%m-%d" and on Linux it worked OK but in FreeBSD says this:
ERROR wrong format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
1
vote
1answer
47 views
SHELL regular expression ends with backslash
I want to check if the pattern ends with "/" or "\" and before it can be any string.
I tried this
*\/
* for any string, \ for removing meaning of "/" and / for "/"
0
votes
1answer
32 views
SHELL— Date format validation
How can I check whether the format of date is correct without date command being printed?
date -d $datum +"%Y-%m-%d"
if [ $? -ne 0 ] ;then
echo error
exit
fi
0
votes
2answers
43 views
What is the variable of $#?
I'm learning introductory Linux and came across something which I did not understand:
"The following command has been executed:
set 10 20 30 40 50
Enter the value of the variable $#:
I understand ...
-4
votes
0answers
15 views
BASH— after getopts continues after exit [on hold]
in getopts(probably) it has two functions in it. One of them has exit at the end and there is also exit at the end of the option b. When I call it like this: ./name.sh -b. (without required argument) ...
1
vote
0answers
22 views
How do processes, jobs and session relate to each other ? [duplicate]
Could you deliver an answer in a way that does not require too much special knowledge in advance, yet also deliver some deeper insight to a lesser advanced Linux User ?
0
votes
1answer
43 views
Shell— print only certain lines from output
I need to choose from output only lines where number is bigger than chosen number.
For example x=27 and I need to find only lines where the number in first column is bigger than that x. In this ...
1
vote
1answer
26 views
How to run a long running grep, pipe the result into less and put everything into background?
I often find myself in the situation where I need to find a code base for a given symbol like so:
find src/galio -not -path './src/stapi-sdk/apilib/* -regextype posix-extended -iregex ...
0
votes
0answers
40 views
Shell splitting line into words without temporary file
yesterday I found out that this creates temporary file. Can anyone help me how to rewrite it so it wouldn't? I am desperate I have tried everything that I know, but it doesn't want to work. And also I ...
3
votes
3answers
84 views
Shell test to find a pattern in a string
I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path.
if [[ $line =~ ...
-1
votes
2answers
59 views
Why doesn't “rm -rf ./” delete anything?
The command rm -rf ./ does not do anything in a directory full of sub directories and files. Why so? Isn't -r supposed to go recursive?
To add more confusion, it even prints an error message ...
2
votes
2answers
61 views
Print file contents and delete the printed content* from the file
In Linux, from a bash shell, how can I print the contents of a file and remove the content I just printed content from the file?
I have a program writing texts to a file repeatedly. On a Linux shell ...
3
votes
1answer
23 views
Trailing colons and dash's builtin test command
What's the meaning of a trailing colon to dash's builtin test (or [) command? I was surprised when I found that if [ ... ]: ; then .. ; fi works (at all) in dash, but not bash, and it seems to be ...
-3
votes
0answers
33 views
Convert file pattern in Unix
I have a file with content as below
31/01/2014
1234
2234
4567
3321
...n
Need to write a shell script to convert the above file as below:
DD_Date='31/01/2014'
...
20
votes
7answers
956 views
`command .*` acts on the parent directory
Someone of our team wanted to recursively change the user permissions on all hidden directories in a users home directory. To do so he executed the following command:
cd /home/username
chown -R ...
0
votes
1answer
49 views
can we get sorted grep output
I have same: 1.txt with apple written in it, 2.txt with mango written in it, 3.txt with apple written in it, 4.txt with mango written in it.
grep -e apple -e mango *.txt will give result as:
1.txt: ...
6
votes
1answer
767 views
What is the meaning of 'ln -sf' in Linux?
I have 2 questions. First one is for the -sf options and second one is for the more specific usage of -f options.
By googling, I figured out the description of command ln, option -s and -f.
(copy ...
2
votes
3answers
54 views
What happens if I pass too few parameters to a shell function?
I would like to ask about passing parameters into functions.
I tried this:
function_name $var1 $var2
but usually (sometimes it printed error) it didn't make any difference whether I passed them or ...
2
votes
3answers
35 views
Command substitutions vs backslash escapes in a quoted string
Consider a double-quoted command substitution with backslash escapes inside it, like this:
echo "$(echo '\\')"
It prints out \\, whereas I would have expected it to print out only one backslash. My ...
0
votes
1answer
27 views
How do i write a shell script so that when i compile my c++ program it will put the binary into the Binary Directory
I have a directory called Binary and bunch of source files:
./
├── Binary/
├── lesson02.cpp
├── lesson04.cpp
└── lesson06.cpp
when I compile the source code
g++ lesson01.cpp -o lesson01
I want ...
1
vote
2answers
42 views
Remove paragraph from file
I have a file (file.php) like this:
...
Match user foo
ChrootDirectory /NAS/foo.info/
ForceCommand internal-sftp
AllowTcpForwarding no
GatewayPorts no
...
1
vote
1answer
53 views
Brace expansion not working in a script
for i in {1..40}
do
echo $i
done
I got
{1..40}
and I would like to have something like
1
2
3
and so on
so I can use the variable i inside a command's parameter.
1
vote
1answer
18 views
Switch-case block inside define keyword in a single logical line in a makefile
This is the code excerpt from ghc.mk, part of Haskell source compilation.
I find it different, because the author has chosen to write one function block in a single line. Is it a common practice in ...
0
votes
4answers
60 views
Copy same specific parts of a file to another file
How can I copy same specific parts of a file to another file? In the first file, I have:
user01: Id name nationality
user02: Id name nationality
.
.
.
I want to copy each piece of information to a ...
2
votes
1answer
36 views
How to verify the target of a symbolic link points toward a particular path
Within a bash script, I know I can check if a file is a symbolic link with the following syntax
if [ -L $path ]
Does any one know how I would test if that path was linked to a particular path? E.g. ...
0
votes
0answers
30 views
How to get and print a user's public IP address in a Shell Script? [duplicate]
I've just completed a lengthy installation script that deploys a Ruby on Rails application unto web servers. At the end of this script, I'd like it to print something along the lines of: "Installation ...
-1
votes
1answer
30 views
Mutiple commands after sudo in one line
I want to execute multiple commands after a sudo command
su - appuser -c "cd /apps/bundle/bundle-client/bin ; export BDL_WEB_USER=not_used ; export BDL_WEB_PASSWORD=not_used ; ./bundle -b ...
1
vote
1answer
31 views
Search for text inside java command output
I have a folder which have multiple Jar files.
* dummy.jar
* myjar-xx.jar
* myjar-yy.jar
Using find, I need to find first file starting with "myjar"
Now, I want to grep the contents of ...
4
votes
1answer
47 views
Why does 'jobs' always return a line for finished processes when run in a subshell within a script?
Normally, when a job is launched in the background, jobs will report that it is finished the first time it is run after the job's completion, and nothing for subsequent executions:
$ ping -c 4 ...
1
vote
1answer
32 views
How do I run a program without network access? [duplicate]
I am making a program that scrapes information from some web services. I want to test how my program reacts when it can't reach the web service, like if the web service is down or if the local ...
2
votes
0answers
29 views
Is there a nice way to set directory/project local environment variables?
I have been working on several projects and they require different environment variables (e.g., PATH for different versions of clang executables, PYTHONPATH for several external modules). Whenever I ...
3
votes
2answers
40 views
Shell globbing without matching files
I frequently make use of shell file name globbing to do things like operate on all of /dev/sda, /dev/sdb, /dev/sdc, etc by doing /dev/sd[a-f], however, this relies on there being files on disk that ...
1
vote
1answer
43 views
Printf formatting with variable format - what does this var reference?
I am experimenting with printf formatting in bash and I ran across this piece of code:
#/bin/bash
divider===============================
divider=$divider$divider
header="\n %-15s %8s %10s %11s\n"
...
0
votes
1answer
40 views
What's a way to get the first network interface using grep in ifconfig? [closed]
Just trying to figure this out for a System Administration introduction course, and I can't seem to get it.
1
vote
4answers
78 views
How do I suppress error messages from cp?
I am currently looking ways to suppress error command in Linux, in particular, the command cp.
I do:
root@ubuntu:~$ cp /srv/ftp/201*/wha*/*.jj ~/.
cp: cannot stat `/srv/ftp/201*/wha*/*.jj': No ...
0
votes
0answers
29 views
Restrict FTP user to access only its own directory and Download Only
I need to create a user which can only FTP to specific directory and download data from it only no upload or delete files.
After searching on line I reached to following points
useradd -g 10 -d ...
-1
votes
0answers
41 views
No Such file or Directory 4 [closed]
throw the script posted in this question link syntax error near unexpected token `)'
I get this error:
test.txt: No such file or directory
140308719470240:error:02001002:system library:fopen:No ...
0
votes
1answer
43 views
Can a command in a shell pipeline determine the tty characteristics of its context?
I have a bash script that columnizes a list of items. By default it will guess at the number of columns to output, based on the terminal width as reported by stty size. But when the script is in a ...
1
vote
1answer
152 views
how to check the string is Null or not in shell script? [closed]
I had a script like this
#!/bin/bash
line="hello"
if [ -z $line ] ; then
echo "String null"
fi
This will work properly, but when I give the lineas follows
...
1
vote
2answers
65 views
Can brace expansion be performed within the `at` command?
In bash, brace expansion is performed as a shell operation before executing the command.1 This means the following will work:
echo {1..10} >output | at -m now
The output will be 1 2 3 4 5 6 7 8 9 ...
0
votes
1answer
25 views
Glob pattern argument for echo command
echo *.c
in bash, treats the argument as a constant and prints *.c. How to force it to consider it as a glob pattern and print the list of files/folders ending with .c?
4
votes
2answers
55 views
Coming back to a command I have partially written [duplicate]
Often I find myself partway through writing a long command (or chain of commands) and realise I need to run another command before I can finish typing out this current one.
Examples
Sometimes, I...
...
2
votes
1answer
26 views
Is there a way to suspend an process belongs to shell A and foreground it in shell B?
I knew how to suspend one process and bring it into foreground in current shell, but I have no idea about that If I have run a process in shell A and now I want to bring it into foreground in current ...