Why is my input not creating a folder? - scripting

I cannot figure out why my script is not creating a folder from my input.
Heres my code ...
#!/bin/bash
echo "Please enter a foldername to store archives in"
read $2
echo "++++++++++++++++++++++++++"
mkdir $2
echo "++++++++++++++++++++++++++"

You use a wrong variable to store the directory name $2 is the second parameter given to your script when you execute it (it could be empty if no second parameter). However i never tested but maybe it could work. The main problem is that for a the read function, you provide the variable without a '$'.
Try to declare a variable.
#!/bin/bash
foldername=""
echo "Please enter a foldername to store archives in"
read foldername
echo "++++++++++++++++++++++++++"
mkdir $foldername
echo "++++++++++++++++++++++++++"

Related

Writing a script in Unix to see if a file exist and to show its content

I'm writing a program in Unix to have the user enter in the file they would like to view the contents on but i'm stuck and dont know way i keep getting error.
the errors i keep getting are :unexpected EOF while looking for matching `"'
and the other is: Syntax error: unexpected end of file
# this program allows the user to see the contents of a file
echo
clear
echo
echo "Enter in the the file you would like to see: "
read $1
if [ ! -e "$1" ]
then
echo cat /export/home/cna397/logname/$1
else
echo "This file does not exist
fi
You're missing an ending double quote here:
echo "This file does not exist
$1 is for commandline arguments. You'll need something like this if you want the user to enter a filename while the script is running:
read filename
echo $(cat "/export/home/cna397/logname/$filename")
this is the executable (without errors) version of what you wrote, now continue with a better basis, if you still have questions just update this post with code plus comments, or just make a new one.
echo "Enter in the the file you would like to see:"
read file_name
if test ! = $file_name
then
echo $(cat /export/home/cna397/logname/$file_name)
else
echo "This file does not exist"
fi
P.S if on the if test you are checking strings, keep the equal (=) i put....

fish shell - Showing the current command in the window title of screen

I want the current command to be shown in the title of screen (or tmux).
I tried following settings but it doesn't work.
How can I make it work?
.screenrc
shelltitle "$ |fish"
shell /usr/local/bin/fish
.config/fish/config.fish
set -x PS1 '\033k\033\\[\u#\h \W]\$ '
For fish version 2.1.0 you only have to edit ~/.config/fish/functions/fish_title.fish
function fish_title
hostname
end
For version 1.23.1 this doesn't seem to work. If the directories do not exist, first create them:
mkdir -p ~/.config/fish/functions/
What worked for me in .config/fish/functions/fish_title.fish :
function fish_title
# this one sets the X terminal window title
# argv[1] has the full command line
echo (hostname): (pwd): $argv[1]
switch "$TERM"
case 'screen*'
# prepend hostname to screen(1) title only if on ssh
if set -q SSH_CLIENT
set maybehost (hostname):
else
set maybehost ""
end
# inside the function fish_title(), we need to
# force stdout to reach the terminal
#
# (status current-command) gives only the command name
echo -ne "\\ek"$maybehost(status current-command)"\\e\\" > /dev/tty
end
end
I think you're looking for fish_title. See documentation here.
You could do something like this:
function fish_title
echo $_ ' '
pwd
end
funcsave fish_title
(Note you just run this at a prompt - don't put it in a config file).
Thanks for your answers.
Finally, this made it work!
.screenrc
shelltitle "$ |fish"
shell /usr/local/bin/fish
.config/fish/config.fish
function fish_prompt
echo -ne '\033k'
echo -ne $argv
echo -ne '\033\\'
echo -ne '$ '
end

awk: setting environment variables directly from within an awk script

first post here, but been a lurker for ages. i have googled for ages, but cant find what i want (many abigious topic subjects which dont request what the topic suggests it does ...). not new to awk or scripting, just a little rusty :)
i'm trying to write an awk script which will set shell env values as it runs - for another bash script to pick up and use later on. i cannot simply use stdout from awk to report this value i want setting (i.e. "export whatever=awk cmd here"), as thats already directed to a 'results file' which the awkscript is creating (plus i have more than one variable to export in the final code anyway).
As an example test script, to demo my issue:
echo $MYSCRIPT_RESULT # returns nothing, not currently set
echo | awk -f scriptfile.awk # do whatever, setting MYSCRIPT_RESULT as we go
echo $MYSCRIPT_RESULT # desired: returns the env value set in scriptfile.awk
within scriptfile.awk, i have tried (without sucess)
1/) building and executing an adhoc string directly:
{
cmdline="export MYSCRIPT_RESULT=1"
cmdline
}
2/) using the system function:
{
cmdline="export MYSCRIPT_RESULT=1"
system(cmdline)
}
... but these do not work. I suspect that these 2 commands are creating a subshell within the shell awk is executing from, and doing what i ask (proven by touching files as a test), but once the "cmd"/system calls have completed, the subshell dies, unfortunatley taking whatever i have set with it - so my env setting changes dont stick from "the caller of awk"'s perspective.
so my question is, how do you actually set env variables within awk directly, so that a calling process can access these env values after awk execution has completed? is it actually possible?
other than the adhoc/system ways above, which i have proven fail for me, i cannot see how this could be done (other than writing these values to a 'random' file somewhere to be picked up and read by the calling script, which imo is a little dirty anyway), hence, help!
all ideas/suggestions/comments welcomed!
You cannot change the environment of your parent process. If
MYSCRIPT_RESULT=$(awk stuff)
is unacceptable, what you are asking cannot be done.
You can also use something like is described at
Set variable in current shell from awk
unset var
var=99
declare $( echo "foobar" | awk '/foo/ {tmp="17"} END {print "var="tmp}' )
echo "var=$var"
var=
The awk END clause is essential otherwise if there are no matches to the pattern declare dumps the current environment to stdout and doesn't change the content of your variable.
Multiple values can be set by separating them with spaces.
declare a=1 b=2
echo -e "a=$a\nb=$b"
NOTE: declare is bash only, for other shells, use eval with the same syntax.
You can do this, but it's a bit of a kludge. Since awk does not allow redirection to a file descriptor, you can use a fifo or a regular file:
$ mkfifo fifo
$ echo MYSCRIPT_RESULT=1 | awk '{ print > "fifo" }' &
$ IFS== read var value < fifo
$ eval export $var=$value
It's not really necessary to split the var and value; you could just as easily have awk print the "export" and just eval the output directly.
I found a good answer. Encapsulate averything in a subshell!
The comand declare works as below:
#Creates 3 variables
declare var1=1 var2=2 var3=3
ex1:
#Exactly the same as above
$(awk 'BEGIN{var="declare "}{var=var"var1=1 var2=2 var3=3"}END{print var}')
I found some really interesting uses for this technique. In the next exemple I have several partitions with labels. I create variables using the labels as variable names and the device name as variable values.
ex2:
#Partition data
lsblk -o NAME,LABEL
NAME LABEL
sda
├─sda1
├─sda2
├─sda5 System
├─sda6 Data
└─sda7 Arch
#Creates a subshell to execute the text
$(\
#Pipe lsblk to awk
lsblk -o NAME,LABEL | awk \
#Initiate the variable with the text for the declare command
'BEGIN{txt="declare "}'\
#Filters devices with labels Arch or Data
'/Data|Arch/'\
#Concatenate txt with itself plus text for the variables(name and value)
#substr eliminates the special caracters before the device name
'{txt=txt$2"="substr($1,3)" "}'\
#AWK prints the text and the subshell execute as a command
'END{print txt}'\
)
The end result of this is 2 variables: Data with value sda6 and Arch with value sda7.
The same exemple in a single line.
$(lsblk -o NAME,LABEL | awk 'BEGIN{txt="declare "}/Data|Arch/{txt=txt$2"="substr($1,3)" "}END{print txt}')

How do I search text in a file with DCL

How do I search text in a file with DCL? Yes, I have to use DCL.
The file format is straight forward:
<NUMBER OF ENTRIES>
<ID> <DIRECTORY>
<ID> <DIRECTORY>
.
.
.
<ID> <DIRECTORY>
They're separated by a few white space characters. I just need to search the file for a given ID and extract the DIRECTORY.
It's a really simple task, but I can't seem to find any decent DCL documentation anywhere.
Edited.... the forum 'eats' strings like <xx> unless marked as code.
Are there pointy brackets on the datalines or not?
Please provide a REAL example
is it
or: XX XXX-DIRECTORY
I am assuming the first.
VMS as it ships does NOT have a standard tool to select a field from a record.
But there are a bunch of standard tools available for OpenVMS which can do this.
Mostly notably (g)AWK and PERL
So that's what I would use:
$ gawk /comm="$1 == ""<xx>"" { print $2 }" tmp.tmp
<xxx-DIRECTORY>
or
$ perl -ne "print $1 if /^\s*<xx>.*?<([^>]*)/" tmp.tmp
xxx-DIRECTORY
Those can be augmented for case-and-space-sensitivity, as needed and trim that <> as needed.
And maybe you need the search ID to be a parameter or not.
Anyway, in a pure DCL script it could look like....
$ IF p2.eqs."" then exit 16
$ CLOSE/NOLOG file
$ OPEN/READ file 'p1
$loop:
$ READ/END=done file rec
$ id = F$EDIT( F$ELEM(0,">",F$ELEM(1,"<",rec)), "UPCASE")
$ IF id.NES.p2 THEN GOTO loop
$ dir = F$ELEM(0,">",F$ELEM(2,"<",rec))
$ WRITE SYS$OUTPUT dir
$ GOTO loop
$done:
$CLOSE/NOLOG file
if the <> do not exist, use this for core...
$ rec = F$EDIT(rec,"TRIM,COMPRESS")
$ id = F$EDI(F$ELEM(0," ",rec),"UPCASE")
$ IF id.NES.p2 THEN GOTO loop
$ dir = F$ELEM(1," ",rec)
And the perl would be:
$ perl -ne "print $1 if /^\s*<xx>\s+(\S+)/" tmp.tmp
Good luck
Hein
Alternatively, if the ID field looks like fixed-width, then you may convert the file to RMS INDEXED , keyed on ID field. Then you can just do lookup by calling READ/KEY='ID'.
Call HELP on CONVERT , READ /KEY and perhaps SEARCH /KEY

How to check return value of Find statment in shell script?

How can I check the return value of "Find" statement in shell script
I am use Find in my script , if find statement don't find any file the execute exit !!
I want to check the return value of "Find" if it found any files or not
You can redirect output of the find command to a file called say output.txt then you can check if the size of that file is 0 or not by using -s option;
if [[ -s "output.txt" ]]
then
echo "File is not empty!"
else
echo "File is empty!"
fi
You can count the number of files found by find using the wc -l command:
export result=`find . -name *.txt | wc -l`
You can now check result to see how many files where found
if [ $result == "0" ]; then echo zero found; fi